web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Merge action using Web API

(0) ShareShare
ReportReport
Posted on by

Hello,

I'm trying to call a Merge action using the Web API, but when I test it, I have the following error:

"Required field 'Target' is missing","innererror".

Here is my request below, I am trying to merge two Contacts as an example:

var parameters = "{";

parameters += "\"Target\": \"/contacts(5037B103-BD07-E611-80FA-5065F38A7A11)\",";

parameters += "\"Subordinate\": \"/contacts(F0E248FD-BC07-E611-80FA-5065F38A7A11)\",";

parameters += "\"PerformParentingChecks\": \"false\",";

parameters += "\"UpdateContent\": \"\"";

parameters += "}";

byte[] MergedContact = Encoding.ASCII.GetBytes(parameters);

string ContactMerge = CrmApiUrl + "Merge";

request = WebRequest.Create(new Uri(ContactMerge)) as HttpWebRequest;

request.ContentType = "application/json; charset=utf-8";

request.Headers.Add("OData-MaxVersion", "4.0");

request.Headers.Add("OData-Version", "4.0");

request.Headers.Add("Authorization", "Bearer " + jsonAuthent.access_token);

request.Method = "POST";

var requestStream3 = request.GetRequestStream();

requestStream3.Write(MergedContact, 0, MergedContact.Length);

requestStream3.Close();

 

HttpWebResponse ContactMergeResponse = request.GetResponse() as HttpWebResponse;

var ContactMergeResponseStream = ContactMergeResponse.GetResponseStream();

 

 

Does anyone has any idea about the syntax for a merge action call?

Thank you in advance,

 

 

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    Hi Emmanuelle,

    Target, Subordinate, UpdateContent use the CRMbaseentity type, you will need to pass through a JSON Object which contains the parameter from the Entity you wish to merge i.e for account - accountid and also a parameter setting the odata type for example with account:

      "@odata.type": "Microsoft.Dynamics.CRM.account"

    Full Request body would look something like:

    {"Target":{

         "accountid": "2fc9ebf0-6b3c-e611-80e0-c4346bc5b2d4",

         "@odata.type": "Microsoft.Dynamics.CRM.account"

    },

    "Subordinate":{

       "accountid": "2fc9ebf0-6b3c-e611-80e0-c4346bc5b2d5,

       "@odata.type": "Microsoft.Dynamics.CRM.account"

       },

    "UpdateContent":{"emailaddress1":"something@hotmail.com",

       "@odata.type": "Microsoft.Dynamics.CRM.account"

    },

    "PerformParentingChecks":"false"

    }

  • Pierre Andre Joubert Profile Picture
    175 on at

    Hi Emmanuelle,

    Were you able to find a solution to this and figure out how to perform the merge?

    Thanks

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Pierre,

    Ian's answer is correct. Also you can check my post that has code that can be used to merge records - butenko.pro/.../merge-records-using-webapi

  • Pierre Andre Joubert Profile Picture
    175 on at

    Hi Andrew,

    Unfortunately I am using c# not JavaScript, I can get retrievals and deletions to work, but sending the Merge request just fails with a Bad Request error, which leads me to believe I have a mistake in my parameters, but I cannot figure out where though:

    string requestPrefix = ApiEndpointPrefix + "contacts(" + item.MergeTargetId + ")" + "/Merge";

    var parameters = new JObject();

    parameters.Add(propertyName: "Target", value: "contacts(" + item.MergeTargetId + ")");

    parameters.Add(propertyName: "Subordinate", value: "contacts(" + item.ContactId + ")");

    parameters.Add(propertyName: "PerformParentingChecks", value: "true");

    //parameters.Add(propertyName: "UpdateContent", value: "");

    httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestPrefix);

    httpRequestMessage.Content = new StringContent(content: parameters.ToString(), encoding: Encoding.UTF8, mediaType: "application/json");

    mergeResponse = await httpClient.SendAsync(httpRequestMessage);

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Pierre,

    C# or JavaScript but payload and urls should be the same. My assumption why you keep getting errors because you use wrong url and different payload. Check article I referenced, fix your code and that should resolve your issue.

  • Pierre Andre Joubert Profile Picture
    175 on at

      

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Pierre,

    Here is solution for your issue.

    1. Url you should use - yourorgnamehere.crm.dynamics.com/.../Merge

    2. Body of Post request:

    {

    "Target": {

    "accountid": "D9C08D12-CE55-E711-80E0-3863BB2E73F0",

    "@odata.type": "Microsoft.Dynamics.CRM.account"

    },

    "Subordinate": {

    "accountid": "E5C08D12-CE55-E711-80E0-3863BB2E73F0",

    "@odata.type": "Microsoft.Dynamics.CRM.account"

    },

    "UpdateContent": {

    "accountid": "00000000-0000-0000-0000-000000000000",

    "@odata.type": "Microsoft.Dynamics.CRM.account"

    },

    "PerformParentingChecks": true

    }

    3. Add to Headers of Request only one - "Content-Type" with value "application/json".

  • Pierre Andre Joubert Profile Picture
    175 on at

    Thanks but the what wasn't the problem, it was the how.

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    I believe that "what" is problem as well because your payload looks like

    {

    "Target": "contacts(someguidhere)",

    "Subordinate": "contacts(someguidhere)",

    "PerformParentingChecks": "true",

    "UpdateContent": ""

    }

    and it's far from payload that I provided in previous reply (tested in Postman and confirmed that it's correct).

    Anyway - do you still have issues with your code?

  • Pierre Andre Joubert Profile Picture
    175 on at

    I knew what I needed to send it was the how, as in how to format it which is what I requested in the first place.

    You provided exactly what was provided previously which didn't help unfortunately, i agreed it was showing the format, but again, the implementation with C# is different to the implementation with JavaScript.  And it is that implementation, the how, that I needed.

    The "how" that has solved the problem, was an example from the Json.Net Documentation site, which guided me to the answer:

                       string requestPrefix = ApiEndpointPrefix + "/Merge";

                       var mergeObject = new JObject(

                           new JProperty(name: "Target", content: new JObject(

                                   new JProperty(name: "contactid", content: item.MergeTargetId),

                                   new JProperty(name: "@odata.type", content: "Microsoft.Dynamics.CRM.contact"))),

                           new JProperty(name: "Subordinate", content: new JObject(

                                   new JProperty(name: "contactid", content: item.ContactId),

                                   new JProperty(name: "@odata.type", content: "Microsoft.Dynamics.CRM.contact"))),

                           new JProperty(name: "UpdateContent", content: new JObject(

                                   new JProperty(name: "contactid", content: "00000000-0000-0000-0000-000000000000"),

                                   new JProperty(name: "@odata.type", content: "Microsoft.Dynamics.CRM.contact"))),

                            new JProperty(name: "PerformParentingChecks", content: "true")

                       );

                       httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestPrefix);

                       httpRequestMessage.Content = new StringContent(content: mergeObject.ToString(), encoding: Encoding.UTF8, mediaType: "application/json");

                       mergeResponse = await httpClient.SendAsync(httpRequestMessage);

    Thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans