Associate primarycontactid to Case-Dynamics CRM web API

核能气质少年 提交于 2020-01-05 04:03:28

问题


I am trying to create a case and associate a contact in the primarycontactId field. I am using an alternate key to look up the contact as below.

POST https://xxxxx.crm.dynamics.com/api/data/v8.2/incidents HTTP/1.1
Accept: application/json
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Host: xxxxxx.crm.dynamics.com
Content-Length: 161

{
  "title": "case101",
  "primarycontactid@odata.bind": "https://xxxxx.crm.dynamics.com/api/data/v8.2/contacts(xxi_xxx='2533274975913147')"
}

Code as below

using (HttpClient httpClient = new HttpClient())
            {
                var method2 = new HttpMethod("POST");
                Uri requesturi = new Uri(string.Format("{0}/api/data/v8.2/", url));
                httpClient.BaseAddress = requesturi;
                httpClient.Timeout = new TimeSpan(0, 0, 4);  // 10 minutes
                httpClient.DefaultRequestHeaders.Accept.Clear();
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                result = GetS2SAccessToken(url, pwd);
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result);
                httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
                httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
                HttpRequestMessage request = new HttpRequestMessage(method,string.Format( "contacts(xxxxx_xxxx='{0}')",xxxx.ToString()));
                request.Content = new StringContent(props.ToString(), Encoding.UTF8, "application/json");
                HttpRequestMessage request2 = new HttpRequestMessage(method2, string.Format("incidents"));
                request2.Content = new StringContent(props1.ToString(), Encoding.UTF8, "application/json");

                HttpResponseMessage createResponse1;
                createResponse1 = await httpClient.SendAsync(request2);
                return createResponse1.Content;
         }

However I get:

An unexpected error occurred.","innererror":{ "message":"An unexpected error occurred.","type":"System.ServiceModel.FaultException.","type":"System.ServiceModel.FaultException1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Crm.Extensibility.OData.CrmODataServiceDataProvider.CreateEdmEntity(CrmODataExecutionContext context, String edmEntityName, EdmEntityObject entityObject, Boolean isUpsert)\r\n at Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]","stacktrace":" at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Create(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode, Dictionary2 optionalParameters)\r\n at Microsoft.Crm.Extensibility.OData.CrmODataExecutionContext.Create(Entity entity)\r\n at Microsoft.Crm.Extensibility.OData.EntityController.PostEntitySet(String entitySetName, EdmEntityObject entityObject)\r\

Has anyone experienced this before?


回答1:


You mixed up. Verify this & correct the schema name.

Incident has 2 optional contact lookups & schema name is primarycontactid & responsiblecontactid.

primarycontactid@odata.bind

But Incident has a mandatory Customer lookup & schema name is customerid. Without this Incident cannot be created. Pass this, request will succeed.

customerid_contact@odata.bind



回答2:


I am not sure if the Web API allows populating a lookup field via an alternate key.

This two step process should work...

  1. Retrieve the Contact's GUID by its alternate key
  2. Populate the lookup using the ID rather than the alternate key, i.e.:

"primarycontactid@odata.bind": "/contacts(F56D5D25-8B0D-E711-8104-00155D6FD705)"



来源:https://stackoverflow.com/questions/45363028/associate-primarycontactid-to-case-dynamics-crm-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!