I have implemented some JavaScript into one of my entities to auto-fill a lookup field. It is on the opportunity entity so can't use a work flow to do this.
Basically, I want to auto fill a lookup field called 'Supplier' on my 'Commission Service' entity when the lookup field 'Existing Product' is filled in - it pulls the supplier from the entity 'Product' which is lookup 'Existing Product'.
I know that the first part of my JS is working as I am able to print out the response onto the console - but I am getting an error:
"LOOKUP CONTROL ERROR: Cannot add item of typename = to the lookup control"
Also, it is not actually doing the job as the supplier field isn't filled in.
Has anyone come across this before? Or can anyone spot the reason why I seem to be getting this error in my code?
My code is below:
function existingProductOnChange() { var response = Xrm.Page.getAttribute("productid").getValue(); var responseId = response[0].id; console.log("Testing Response:" + responeId); getSupplier(responseId); } function getSupplier(responseId){ var req = new XMLHttpRequest(); var url = "/api/data/v8.0/products()?$select=_new_supplier_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; charset=utf-8"); req.setRequestHeader("Prefer", "odata.include-annotations=\'*\'"); req.onreadystatechange = null; console.log("TESTING 1"); if (this.status === 200){ var result = JSON.parse(this.response); var supplierID = result["_new_supplier_value"] var supplierName = result["_new_supplier_value@OData.community.Display.V1.FormattedValue"]; console.log("TESTING 2"); SetLookupField("new_supplier", supplierID, supplierName, "new_supplier"); console.log("TESTING 3"); } else { Xrm.Utility.alertDialog(this.statusText); console.log("TESTING 4"); } } }; 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); }
*This post is locked for comments