Search code examples
dynamics-crm-2011dynamics-crmcrm

In CRM 2011 how to create a record and associate it in the same ExecuteMultipleRequest


I'm struggling to find a way to use the ExecuteMultipleRequest to create a record and associate it to other records at the same time. So far I've only been successful in first creating the record, and then in another request set up my associations.

With this code it throws an exception that it requires a target (Which would need to have previously been submitted to create the record and get an 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;
}

Solution

  • The only way to achieve with ExecuteMultipleRequest is to specify the Id of the new record before you create the record:

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

    Specify the Id is permitted but not suggested.