简体   繁体   English

将 primarycontactid 关联到 Case-Dynamics CRM Web API

[英]Associate primarycontactid to Case-Dynamics CRM web API

I am trying to create a case and associate a contact in the primarycontactId field.我正在尝试创建一个案例并在 primarycontactId 字段中关联一个联系人。 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.FaultException 1[[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, Dictionary 2 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\发生意外错误1[[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, Dictionary 2 optionalParameters)\r\n Microsoft.Crm.Extensibility.OData.CrmODataExecutionContext.Create(实体实体)\r\n 在 Microsoft.Crm.Extensibility.OData.EntityController.PostEntitySet(String entitySetName, EdmEntityObject entityObject)\r\

Has anyone experienced this before?有谁之前经历过这个吗?

You mixed up.你混淆了。 Verify this & correct the schema name.验证这一点并更正模式名称。

Incident has 2 optional contact lookups & schema name is primarycontactid & responsiblecontactid.事件有 2 个可选的联系人查找和模式名称是 primarycontactid 和 responsiblecontactid。

primarycontactid@odata.bind

But Incident has a mandatory Customer lookup & schema name is customerid .但是事件有一个强制性的客户查找和模式名称是customerid Without this Incident cannot be created.没有这个事件就无法创建。 Pass this, request will succeed.通过这个,请求就会成功。

customerid_contact@odata.bind

I am not sure if the Web API allows populating a lookup field via an alternate key.我不确定 Web API 是否允许通过备用键填充查找字段。

This two step process should work...这两个步骤的过程应该工作...

  1. Retrieve the Contact's GUID by its alternate key通过备用键检索联系人的 GUID
  2. Populate the lookup using the ID rather than the alternate key, ie:使用 ID 而不是备用键填充查找,即:

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

Try with this in the request body, It will help.在请求正文中尝试这个,它会有所帮助。

"primarycontactid@odata.bind":"/contacts(alternate_key_field='2222222')"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM