Hi,
I have changed the parameter as advised and still it is not getting map. Running this code on form Onload Also there is no any error. When click on new lead, Business Unit Field remain empty.
JFYI - Code
function setBusinessUnit() {
var loggedInUserId = Xrm.Page.context.getUserId();
loggedInUserId = loggedInUserId.replace('{', '').replace('}', '');
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemusers(" + loggedInUserId + ")?$select=_businessunitid_value", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
var _businessunitid_value = result["_businessunitid_value"];
var _businessunitid_value_formatted = result["_businessunitid_value@OData.Community.Display.V1.FormattedValue"];
var _businessunitid_value_lookuplogicalname = result["_businessunitid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
SetLookUp(new_businessunit, _businessunitid_value_lookuplogicalname, _businessunitid_value, _businessunitid_value_formatted);
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
// *** FUNCTION: SetLookUp
// *** PARAMS:
// *** fieldName = The name of the lookup field
// *** fieldType = The type of field (contact, account etc)
// *** fieldId = The ID of the value to set (GUID)
// *** value = the value(name) to set
function SetLookUp(new_businessunit, fieldType, fieldId, value) {
try {
var object = new Array();
object[0] = new Object();
object[0].id = fieldId;
object[0].name = value;
object[0].entityType = fieldType;
Xrm.Page.getAttribute(new_businessunit).setValue(object);
}
catch (e) {
alert("Error in SetLookUp: fieldName = " + fieldName + " fieldType = " + fieldType + " fieldId = " + fieldId + " value = " + value + " error = " + e);
}
}