
I am looking to map a lookup field to another lookup field, similar to how it is done on this blog http://himbap.com/blog/?p=2852
However, my Java skills are not up to the task of modifying the code for my situation. Thankfully I need only map one field.
My situation is as follows:
The action I am looking for is as follows
I cannot use a Workflow to solve the problem, as the "Commissioned Services" entity is a modification of opportunity_product which does not allow Workflows to be run on it, and the problem cannot be solved using the mapping 1 to N facility either.
Thanks in anticipation
Jimbo
*This post is locked for comments
I have the same question (0)The article that you reference is using Xrm api for Dynamics 365 v9, so it doesn't apply to your requirement.
You can still use webapi though to get the data into your entity:
Basically what you need to do is create the OnChange event of the Procurement Responses field on the Commissioned Services entity.
Within the OnChange event, you would get the Id of the lookup that was just added/modified, and would call an api retrieve function on the Procurement Responses entity and retrieve the SupplierName field.
Here are some pointers on how to do this (all of this would be on the script file/web resource that is attached to the Commissioned Services entity):
function procurementResponseOnChange()
{
var response = Xrm.Page.getAttribute("new_procurementresponse").getValue();
var responseId = response[0].id;
getSupplier(responseId);
}
function getSupplier(responseId)
{
var req = new XMLHttpRequest();
var url = "/api/data/v8.0/new_procurementresponses()?$select=_new_suppliername_value";
req.open("GET", Xrm.Page.context.getClientUrl() + url, false);
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 supplierId = result["_new_suppliername_value"];
var supplierName = result["_new_suppliername_value@OData.Community.Display.V1.FormattedValue"];
// new_supplier is the lookup entity name
SetLookupField("new_suppliername", supplierId, supplierName, "new_supplier");
}
else
{
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
function setLookupField(fieldName, lookupId, lookupName, entityName) {
var lookupData = new Array();
var lookupItem = new Object();
lookupItem.id = lookupId;
lookupItem.name = lookupName;
lookupItem.entityType = entityName;
lookupData[0] = lookupItem;
Xrm.Page.getAttribute(fieldName).setValue(lookupData);
}
You will need to check/debug this code, but this should be the logic of how to get it done.
Hope this helps.