locked
Can I Update Multiple records Using EntityCollection in single call RRS feed

  • Question

  •   I have a requirement to update multiple records in single call, I am using serialized EntityCollection which being provided as input to my code. Now I deserialize this in to EntityCollection Object and call a Update method on it. But Update will not accept EnitityCollecton, only Entity is allowed. Can any one tell me how to handle this.

    Following is code snippet.

    EntityCollection deserializedContact = null;
    dataContractSerializer = new DataContractSerializer(typeof(EntityCollection));
    var earlyBoundSerializer = dataContractSerializer;
    using (var file = new FileStream(path, FileMode.Open))
    {
    	deserializedContact = (EntityCollection)earlyBoundSerializer.ReadObject(file);
    
    }
    
    Guid id = new Guid();
    XmlDocument createdEntity = new XmlDocument();
    string createdEntityXMLPath = string.Empty;
    try
    {
        if (operation == "UPDATE")
        {
    		client.Update(deserializedContact); //This will not accept EntityCollection
        }
                   
    }
    catch (Exception ex)
    {
    
    }


    Thanks Regards Prashanth Kamasamudram Even the least work done for others awakens the power within; even thinking the least good of others gradually instills into the heart the strength of a lion.


    Thursday, January 16, 2014 1:37 PM

Answers

  • The Update method will only accept an Entity as a parameter.

    There are two ways to pass multiple records to be updated in one call:

    • Use the ExecuteMultiple method, with an UpdateRequest for each record you want to update. This is available in Crm 201, or Crm 2011 UR12 or higher
    • If all the records have a Many-One relationship to one record (for example, if they have the same owner), then you can do an Update on the parent record, with all the other records in the RelatedEntities property

    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk


    Friday, January 17, 2014 8:39 AM
    Moderator
  • Thank you David

         I got solution for this. as you mentioned in your response, I have used ExecuteMultiple. Its working..

        In case if anyone is looking out for same approach, following is the code snippet I have used.

           
     public void executeMulipleUpdate(Microsoft.Xrm.Sdk.EntityCollection entities,IOrganizationService service)
    {
    
        ExecuteMultipleRequest multipleRequest = new ExecuteMultipleRequest()
        {
            Settings = new Microsoft.Xrm.Sdk.ExecuteMultipleSettings()
            {
                  ContinueOnError = true,
                  ReturnResponses = true
            },
            Requests = new Microsoft.Xrm.Sdk.OrganizationRequestCollection()
        };
    
        foreach (Microsoft.Xrm.Sdk.Entity e in entities.Entities)
        {
            //In this instance, we use a CreateRequest, although there are also options for UpdateRequest and DeleteRequest		
            UpdateRequest createRequest = new UpdateRequest(); ///Here we can change UpdateRequest To CreateRequest for create
            //Point it at the entity to insert
            createRequest.Target = e;
            //Add the entity to the ExecuteMultipleRequest
            multipleRequest.Requests.Add(createRequest);
        }
                
        //This is how to simply execute the command without any response. below is how to use get responses from the request. 
        //service.Execute(multipleRequest);
    
        //This is how to get responses
        ExecuteMultipleResponse executeResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest);
    
        XmlDocument MultipleResponseXML=SerializeEntityCollectionResponceToXML(executeResponse);
    
        if (executeResponse.Results != null)
        {
             //Loop through responses
            foreach (ExecuteMultipleResponseItem responseItem in executeResponse.Responses)
            {
                        if (responseItem.Response != null)
                        {
                            //command completed as expected
                            //success++;
                        }
                        else if (responseItem.Fault != null)
                        {
                            //error occoured
                            //error++;
                        }
                        else
                        {
                            //Error reporting error;
                        }
            }
        }
    }

    Thank Regards

    Prashanth


    Thanks Regards Prashanth Kamasamudram Even the least work done for others awakens the power within; even thinking the least good of others gradually instills into the heart the strength of a lion.


    Friday, January 17, 2014 8:47 AM

All replies

  • The Update method will only accept an Entity as a parameter.

    There are two ways to pass multiple records to be updated in one call:

    • Use the ExecuteMultiple method, with an UpdateRequest for each record you want to update. This is available in Crm 201, or Crm 2011 UR12 or higher
    • If all the records have a Many-One relationship to one record (for example, if they have the same owner), then you can do an Update on the parent record, with all the other records in the RelatedEntities property

    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk


    Friday, January 17, 2014 8:39 AM
    Moderator
  • Thank you David

         I got solution for this. as you mentioned in your response, I have used ExecuteMultiple. Its working..

        In case if anyone is looking out for same approach, following is the code snippet I have used.

           
     public void executeMulipleUpdate(Microsoft.Xrm.Sdk.EntityCollection entities,IOrganizationService service)
    {
    
        ExecuteMultipleRequest multipleRequest = new ExecuteMultipleRequest()
        {
            Settings = new Microsoft.Xrm.Sdk.ExecuteMultipleSettings()
            {
                  ContinueOnError = true,
                  ReturnResponses = true
            },
            Requests = new Microsoft.Xrm.Sdk.OrganizationRequestCollection()
        };
    
        foreach (Microsoft.Xrm.Sdk.Entity e in entities.Entities)
        {
            //In this instance, we use a CreateRequest, although there are also options for UpdateRequest and DeleteRequest		
            UpdateRequest createRequest = new UpdateRequest(); ///Here we can change UpdateRequest To CreateRequest for create
            //Point it at the entity to insert
            createRequest.Target = e;
            //Add the entity to the ExecuteMultipleRequest
            multipleRequest.Requests.Add(createRequest);
        }
                
        //This is how to simply execute the command without any response. below is how to use get responses from the request. 
        //service.Execute(multipleRequest);
    
        //This is how to get responses
        ExecuteMultipleResponse executeResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest);
    
        XmlDocument MultipleResponseXML=SerializeEntityCollectionResponceToXML(executeResponse);
    
        if (executeResponse.Results != null)
        {
             //Loop through responses
            foreach (ExecuteMultipleResponseItem responseItem in executeResponse.Responses)
            {
                        if (responseItem.Response != null)
                        {
                            //command completed as expected
                            //success++;
                        }
                        else if (responseItem.Fault != null)
                        {
                            //error occoured
                            //error++;
                        }
                        else
                        {
                            //Error reporting error;
                        }
            }
        }
    }

    Thank Regards

    Prashanth


    Thanks Regards Prashanth Kamasamudram Even the least work done for others awakens the power within; even thinking the least good of others gradually instills into the heart the strength of a lion.


    Friday, January 17, 2014 8:47 AM