I'm new to Dynamics and JavaScript. I've created a custom entity, used CRM REST Builder to generate my request, and added an event to a lookup. I'd like to run two different queries, combine the results, and update the new_name field. Below is my code for the first query. How do I get and use the results outside of the if statement? Also, how would I pass the results to another function?
function updateName(){
var productLookupObject = Xrm.Page.getAttribute("new_productid");
if (productLookupObject != null) {
var productLookupObjectValue = productLookupObject.getValue();
if (productLookupObjectValue != null) {
var entityID = productLookupObjectValue[0].id;
entityID = entityID.replace(/[{}]/gi, '');
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_customproducts(" + entityID + ")?$select=new_productid", 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 new_productid = result["new_productid"];
Xrm.Page.getAttribute("new_name").setValue(new_productid);
} else {
var error = JSON.parse(this.response).error;
alert(error.message);
}
}
};
req.send();
}
}
}
Thanks,
Ryan
*This post is locked for comments
I have the same question (0)