locked
ExecuteMultipleRequest for bulk Create/Update RRS feed

  • Question

  • Hi,
    I am working on a SSIS package which imports contacts in batches.
    So far I was able to create contacts with createRequest and is working fine. But I am not sure how to do a updateRequest in the same package.
    Someone please help me. THanks.

    Here is my code for createRequest and now I am trying to update the contacts but not sure how to create an updateRequest.

    public override void Input0_ProcessInput(Input0Buffer Buffer)
        {
            int index = 0;
            while (Buffer.NextRow())
            {
                _contacts.Add(GetContactFromBuffer(Buffer));
                index++;
    
                // Let's use buffer size 500. CRM allows up to 1000 requests per single call
                if (index == 500)
                {
                    ImportBatch();
                    index = 0;
                }
            }
            ImportBatch();
        }
    
        private void ImportBatch()
        {
            if (_contacts.Count > 0)
            {
                // Create and configure multiple requests operation
                var multipleRequest = new ExecuteMultipleRequest()
                {
                    Settings = new ExecuteMultipleSettings()
                    {
                        ContinueOnError = true, // Continue, if processing of a single request fails
                        ReturnResponses = true // Return responses so you can get processing results
                    },
                    Requests = new OrganizationRequestCollection()
                };
    
                // Build a CreateRequest for each record
                foreach (var contact in _contacts)
                {
                    CreateRequest reqCreate = new CreateRequest();
                    reqCreate.Target = contact;
                    reqCreate.Parameters.Add("SuppressDuplicateDetection", false); // Enable duplicate detection 
                    multipleRequest.Requests.Add(reqCreate);
                }
    
                ExecuteMultipleResponse multipleResponses = (ExecuteMultipleResponse)_orgService.Execute(multipleRequest);    
     
            _contacts.Clear();
            }
        }
    	 private Entity GetContactFromBuffer(Input0Buffer Row)
        {
    	//To update Contacts
    	  bool hasDulicate = false;
    	   QueryExpression query = new QueryExpression();
           query.EntityName = "contact";
           query.ColumnSet = new ColumnSet(true);
           query.Criteria.AddCondition("new_uniqueid", ConditionOperator.Equal, Row.UniqueName);
    	  
    	  EntityCollection records = _orgService.RetrieveMultiple(query);
    
              if (records.Entities.Count == 1)
              {
    		  Entity existingContact = new Entity("contact");
    
              existingContact.Id = records.Entities[0].Id;
    		  if (!Row.FirstName_IsNull || Row.FirstName_IsNull)
                {
                   existingContact["firstname"] = Row.FirstName;
                }
    
                if (!Row.LastName_IsNull || Row.LastName_IsNull)
                {
                    existingContact["lastname"] = Row.LastName;
                }
    			return existingContact;
    			hasDulicate = true;
    			
    			//Create new contacts
    			if (hasDulicate == false)
    			{
    	          Entity contact = new Entity("contact");
    
    	        if (!Row.FirstName_IsNull)
                {
                    contact["firstname"] = Row.FirstName;
                }
    
                if (!Row.LastName_IsNull)
                {
                    contact["lastname"] = Row.LastName;
                }
    			 return contact;
    		    }
    	}

    Thank you.




    • Edited by ReignFan Saturday, May 16, 2015 2:20 PM
    Saturday, May 16, 2015 2:00 PM

All replies

  • Update request is the almost the same just Entity with Id and with attributes you want to update:

    Entity existingContact = new Entity("contact");
    existingContact.Id = records.Entities[0].Id;
    existingContact["lastname"] = Row.LastName;
    
    UpdateRequest reqUpdate = new UpdateRequest { Target = existingContact };
    multipleRequest.Requests.Add(reqUpdate);

    Sunday, May 17, 2015 4:52 PM
  • Thanks Ambecki,

    I tried the same but nothing is happening on execution. Also, visual studio not letting me to debug. It opens for a second on the breakpoint and closes. What am I doing wrong. Please help me.

    public override void Input0_ProcessInputRow(Input0Buffer Buffer)
        {
            int index = 0;
            while (Buffer.NextRow())
            {
                _contacts.Add(GetContactFromBuffer(Buffer));
                index++;
    
                // Let's use buffer size 500. CRM allows up to 1000 requests per single call
                if (index == 500)
                {
                    ImportBatch();
                    index = 0;
                }
            }
            ImportBatch();
        }
    
        private void ImportBatch()
        {
            if (_contacts.Count > 0)
            {
                // Create and configure multiple requests operation
                var multipleRequest = new ExecuteMultipleRequest()
                {
                    Settings = new ExecuteMultipleSettings()
                    {
                        ContinueOnError = true, // Continue, if processing of a single request fails
                        ReturnResponses = true // Return responses so you can get processing results
                    },
                    Requests = new OrganizationRequestCollection()
                };
    
                // Build a UpdateRequest for each record
                foreach (var contact in _contacts)
                {
                    UpdateRequest reqUpdate = new UpdateRequest();
                    reqUpdate.Target = existingContact;
                    reqUpdate.Parameters.Add("SuppressDuplicateDetection", false); // Enable duplicate detection 
                    multipleRequest.Requests.Add(reqUpdate);
                }
    
                ExecuteMultipleResponse multipleResponses = (ExecuteMultipleResponse)_orgService.Execute(multipleRequest);
    
                _contacts.Clear();
            }
        }
    
    private Entity GetContactFromBuffer(Input0Buffer Row)
        {
            ////Update the records from UJSPOrtal to CallManager
           
            QueryExpression query = new QueryExpression();
            query.EntityName = "contact";
            query.ColumnSet = new ColumnSet(true);
            query.Criteria.AddCondition("new_uniqueid", ConditionOperator.Equal, Row.UserId);
    
            EntityCollection records = _orgService.RetrieveMultiple(query);
    
            Entity existingContact = new Entity("contact");
    
            if (records.Entities.Count == 1)
            {       
               existingContact.Id = records.Entities[0].Id;
    
                if (!Row.UserId_IsNull || Row.UserId_IsNull)
                {
                    existingContact["new_uniqueid"] = Row.UserId;
                }
    
                if (!Row.FirstName_IsNull || Row.FirstName_IsNull)
                {
                    existingContact["firstname"] = Row.FirstName;
                }
           }        
            return existingContact;
    }
    
    Thank you.



    • Edited by ReignFan Friday, May 22, 2015 1:52 AM
    Friday, May 22, 2015 1:51 AM
  • Sorry for delay...Just reading I see existingContact in loop:

    // Build a UpdateRequest for each record
                foreach (var contact in _contacts)
                {
                    UpdateRequest reqUpdate = new UpdateRequest();
                    reqUpdate.Target = contact; // <- this line
                    reqUpdate.Parameters.Add("SuppressDuplicateDetection", false); // Enable duplicate detection 
                    multipleRequest.Requests.Add(reqUpdate);
                }
    
    If you don't have possibility to debug code try to create single update request and use Trace:
    https://crmbusiness.wordpress.com/2011/02/08/crm-2011-you-can-now-add-tracing-to-your-plugins/

    Sunday, May 31, 2015 3:44 PM