Tuesday, September 25, 2012

CRM 2011 - Get business unit using whoami request in XrmServiceToolkit

function GetBusinessUnit() {
var currentstore=Xrm.Page.getAttribute('jmh_store').getValue();
  if(currentstore == null)
{
    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 buName = retrievedContact.attributes['name'].value;
    SetLookupValue('jmh_store', buid, buName, 'businessunit');
  }

function SetLookupValue(fieldName, id, name, entityType) {
    if (fieldName != null) {
        var lookupValue = new Array();
        lookupValue[0] = new Object();
        lookupValue[0].id = id;
        lookupValue[0].name = name;
        lookupValue[0].entityType = entityType;
        Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
    }
}
}

Note : Add XrmServiceToolkit in the page library
Reference: http://xrmservicetoolkit.codeplex.com/wikipage?title=Soap%20Functions

No comments:

Post a Comment