//for example set opportunity amount instade of opportunity name
function setOpportunityAmount() {
if(Xrm.Page.getAttribute("new_oppamount").getValue()==null){
return;
}
var opportunity_id = Xrm.Page.getAttribute("new_oppamount").getValue()[0].id;
opportunity_id = opportunity_id.replace("}", "").replace("{", "");
get_OpprAmount(opportunity_id).then(function(result) {
var new_oppamount = result["new_oppamount"];
if (!!new_oppamount) {
Xrm.Page.getAttribute("new_oppamount").setValue([{
id: opportunity_id,
name: new_oppamount,
entityType: 'opportunity'
}]);
}
}
).catch(error => Alert.show(error));
}
function get_OpprAmount(opportunity_id) {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/opportunities(" + opportunity_id + ")?$select=new_oppamount", 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);
resolve(result);
} else {
reject(this.statusText);
}
}
}
req.send();
});
}
No comments:
Post a Comment