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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Dynamics 365 Web API C# : Attempting to Merge contact records and failing with BAD REQUEST error.

(0) ShareShare
ReportReport
Posted on by 175

Hi,

I have been struggling to find a solution to this error. Unfortunately I get nothing more than "BAD REQUEST" in the response message.  While I understand that it is eluding to a parameter issue, but... which one and where, is what I cannot figure out.

I am creating, for now, a console app to merge contact records using the Web API.

So... Here is the code I am using at the moment, that returns the annoyingly 'useful' error:

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);


Any thoughts or suggestions are greatly appreciated as I have searched bing and google high and low and cannot find any examples of how to use the Merge action with the API and according to the documentation on the Merge Action I have found it should be simple... but alas, I continue to struggle.

Thanks

Pierre

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Pierre Andre Joubert Profile Picture
    175 on at

    Figured out the solution to my issue, thanks to the Json.Net documentation.  Basically this is the format that is needed.

    Hope this helps someone else and removes any frustration.

                       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);

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

News and Announcements

Season of Giving Solutions is Here!

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
Shidin Haridas Profile Picture

Shidin Haridas 2

#2
Abdullah13 Profile Picture

Abdullah13 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans