Hello everyone!
I need some help with javascript. I have a requirement such that if I select a value from one lookup say Member and if their is only one Client associated with that Member then the value in the Client lookup field must auto populate. If there are more than one Client lookup associated with the Member then it must provide a list of all the look up values. Note that both the lookup fields are present on the same form.
I have made some progress with the code, but my filtering is not happening as it should be. If anyone could 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();
}