我正在努力寻找一种方法来使用ExecuteMultipleRequest来创建记录并同时将其与其他记录相关联。到目前为止,我只是成功地首先创建了记录,然后在另一个请求中建立了我的关联。
使用此代码会抛出一个异常,它需要一个目标(需要先提交以创建记录并获取ID)
private AssociateRequest GetConfigurationRequest(VariableDataRequestModel receivedRequest)
{
var configurationRelationship = new Relationship(ConfigurationRelationshipName);
var configurationEntities = new EntityReferenceCollection(new EntityReference[]{new EntityReference(ConfigurationEntityLogicalName,new Guid(receivedRequest.ConfigurationId))});
var rtn = new AssociateRequest() { RelatedEntities = configurationEntities, Relationship = configurationRelationship };
return rtn;
}
答案 0 :(得分:5)
使用ExecuteMultipleRequest
实现的唯一方法是在创建记录之前指定新记录的Id
:
Guid accountId = new Guid("c85dca5d-6520-4436-a5f6-178b633af819");
Entity myAccount = new Entity("account");
myAccount["name"] = "TEST ACCOUNT";
entity.Id = accountId;
// ...
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
relatedEntities.Add(new EntityReference("account", accountId));
指定允许Id
,但不建议。
答案 1 :(得分:0)
您可以做的另一件事是在ExecuteMultipleRequest中提交记录,然后在ExecuteMultipleResponse中获取GUID,然后您可以将该GUID与您创建的Record一起使用来执行AssociateRequest。
示例:
ExecuteMultipleResponse response = (ExecuteMultipleResponse)service.Execute(updateCRM);
List<Entity> addedRecordList = new List<Entity>();
foreach (ExecuteMultipleResponseItem item in response.Responses)
{
Guid responseReturnedGuid = (Guid)item.Response.Results.Values.FirstOrDefault(x => x is Guid);
if (responseReturnedGuid != Guid.Empty || responseReturnedGuid != null)
{
Entity addedRecord = (Entity)updateCRM.Requests[item.RequestIndex].Parameters.Values.FirstOrDefault(x => x is Entity);
addedRecord.Id = responseReturnedGuid;
addedRecordList.Add(addedRecord);
}
}