Monday 21 January 2013

Get Current user business unit in CRM 2011 using javascript


function GetBusinessUnit() {  
        var request = "<request i:type='b:WhoAmIRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' xmlns:b='http://schemas.microsoft.com/crm/2011/Contracts'>" +
                            "<a:Parameters xmlns:c='http://schemas.datacontract.org/2004/07/System.Collections.Generic' />" +
                            "<a:RequestId i:nil='true' />" +
                            "<a:RequestName>WhoAmI</a:RequestName>" +
                          "</request>";
        var resultXml = XrmServiceToolkit.Soap.Execute(request);
        var buid = resultXml.getElementsByTagName("a:Results")[0].childNodes[1].childNodes[1].text;
        var cols = ["name"];
        var retrievedContact = XrmServiceToolkit.Soap.Retrieve("businessunit", buid, cols);
        var businessUnitName = retrievedContact.attributes['name'].value;
       alert(businessUnitName);
    }
After that you need to add the following files to the Form Libraries.

  1. Jquery
  2. Json2
  3. XrmServiceToolkit
Download these files from the following link: XrmServiceToolkit

9 comments:

  1. Hi Kanagaraj, I received "microsoft jscript runtime error: undefined", Please let me know the solution

    ReplyDelete
  2. The above error is in CRM which is IFD. since it is IFD, the url is different when compared to its original URL.
    I facing problem on this, please let me know the solution for this.

    ReplyDelete
    Replies
    1. Did you try to debug your code and checked in which line you are getting this error?

      Delete
    2. yes, error was in the below line,
      var resultXml = XrmServiceToolkit.Soap.Execute(request);
      But, It is working fine in my another CRM.
      Problem is as i told it is an IFD CRM

      Delete
    3. Maybe it is a bug of XrmServiceToolkit,you can solved it by call getClientUrl instead of getServiceUrl,here(http://xrmservicetoolkit.codeplex.com/discussions/438589)is the link discuss about this

      Delete
  3. I'm getting this error....

    Field:window

    Event:onload

    Error:The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://schemas.microsoft.com/crm/2011/Contracts/Services:id. The InnerException message was 'There was an error deserializeing the object of type System.Guid. The value '' cannot be parsed as the type 'Guid'.'. Please see InnerException for more details.

    Huh?

    ReplyDelete
  4. Just traced the error.

    Here is the fix:

    var buid = resultXml.getElementsByTagName("a:Results")[0].childNodes[1].childNodes[1].textContent;

    ReplyDelete
  5. Hi,
    it worked for me
    thanks

    ReplyDelete
  6. Easy way

    function GetUserBS()
    {
    var ContactID = Xrm.Page.context.getUserId();
    Cols = ["businessunitid"];
    var Retrieved = XrmServiceToolkit.Soap.Retrieve ("systemuser", ContactID, Cols );

    var UnitObj = Retrieved.attributes["businessunitid"];
    var UnitName = UnitObj.name

    return (UnitName);
    }

    ReplyDelete