Development, Howto

How to Merge records using WebApi

Following code can be used to merge records using JavaScript and WebApi:

var target = {
    leadid: "E5975EA3-531C-E511-80D8-3863BB3CE2C8"//Use contactid, accountid or incidentid to merge other entities
};
target["@odata.type"] = "Microsoft.Dynamics.CRM.lead";//use contact, account or incident for other entities

var subordinate = {
    leadid: "ED975EA3-531C-E511-80D8-3863BB3CE2C8"//Use contactid, accountid or incidentid to merge other entities
};
subordinate["@odata.type"] = "Microsoft.Dynamics.CRM.lead";//use contact, account or incident for other entities

var updatecontent = {
    leadid: "00000000-0000-0000-0000-000000000000"//Use contactid, accountid or incidentid to merge other entities
};
updatecontent["@odata.type"] = "Microsoft.Dynamics.CRM.lead";//use contact, account or incident for other entities

var parameters = {};
parameters.Target = target;
parameters.Subordinate = subordinate;
parameters.UpdateContent = updatecontent;
parameters.PerformParentingChecks = true;

var context;

if (typeof GetGlobalContext === "function") {
    context = GetGlobalContext();
} else {
    context = Xrm.Page.context;
}

var req = new XMLHttpRequest();
req.open("POST", context.getClientUrl() + "/api/data/v8.2/Merge", 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.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 204) {
            //Success - No Return Data - Do Something
        } else {
            var errorText = this.responseText;
            //Error and errorText variable contains an error - do something with it
        }
    }
};
req.send(JSON.stringify(parameters));

 

3 Comments

  1. Is there an example of this using OData API for Java ?

    I am constructing ClientEntity objects which get parsed to InputStream and sent to web api. This library attaches {fieldname@odata.type, fieldname:fieldvalue}

    Not sure how to pass in this request using OData

    1. Hello Vicky,
      I’m afraid that I’m not an expert in Java to suggest.
      Thanks,
      Andrew

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.