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