Hi Rushikesh,
First of all thanks for all the help. You advice has come very much to use. I am new in this field and dont have much knowledge of MS CRM.
I have a new requirement where the value of one field must autopopulate when I select a value in another lookup. For example I have two custom entities Member and Client which have an association with each other. When I select a value in the Member field, the Client field must autopopulate if there is only one client associated with the member, if there are more than one clients it must give me a list of all the related clients(similar to filetered lookups).
I have written a code but have not got the desired output. If you could please help me with the same.
function memberClient()
{
var entityName, entityId, entityLabel, lookupFieldObject;
lookupFieldObject = Xrm.Page.data.entity.attributes.get('new_member');
if (lookupFieldObject.getValue() != null)
{
entityId = lookupFieldObject.getValue()[0].id;
entityId = entityId.replace('{', '').replace('}', '');
entityName = lookupFieldObject.getValue()[0].entityType;
entityLabel = lookupFieldObject.getValue()[0].name;
alert(entityId);
alert(entityName);
alert(entityLabel);
}
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/new_memberclientassociations?$select=_new_client_value&$filter=_new_member_value eq " + entityId, 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=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var _new_client_value = results.value[i]["_new_client_value"];
var _new_client_value_formatted = results.value[i]["_new_client_value@OData.Community.Display.V1.FormattedValue"];
alert(_new_client_value_formatted);
alert(_new_client_value);
var lookup = new Array();
lookup[0] = new Object();
lookup[0].id = _new_client_value;
lookup[0].name = _new_client_value_formatted;
lookup[0].entityType = new_client;
Xrm.Page.getAttribute("new_client").setValue(lookup);
}
}
else {
alert(this.statusText);
}
}
};
req.send();
}