Monday, May 2, 2016

Bulk Create in MSCRM 2015 Plugins

Hi All,

Please find the below Script to Bulk Update Records. In My Scinario am having Entity1 and Entity2 entities.
If i update Entity1 Status Entity2 Status also should get update with the same Status.

Code:

 ExecuteMultipleRequest requestWithResults = new ExecuteMultipleRequest()
                    {                    
                        Settings = new ExecuteMultipleSettings()
                        {
                            ContinueOnError = true,
                            ReturnResponses = true
                        },
                        Requests = new OrganizationRequestCollection()
                    };

QueryExpression qe = new QueryExpression(Entity2);
qe.ColumnSet = new ColumnSet(true);
qe.Criteria.AddCondition(new ConditionExpression("Entity1id", ConditionOperator.Equal, {GUID OF Entity1} ));
EntityCollection collectAllValues = service.RetrieveMultiple(qe);

 foreach (Entity loopCollectedValues in collectAllValues.Entities)
            {
                Entity getAllrecords = new Entity("Entity2");

                getAllrecords.Id = Entity2.Id;

               getAllrecords.Attributes["new_status"] = new OptionSetValue(((Microsoft.Xrm.Sdk.OptionSetValue)((postImageEntity.Attributes["new_status"]))).Value);

                CreateRequest createRequest = new CreateRequest { Target = getAllrecords  };

                requestWithResults.Requests.Add(createRequest);
            }

ExecuteMultipleResponse responseWithResults =
(ExecuteMultipleResponse)_service.Execute(requestWithResults);
if (responseWithResults.IsFaulted)
                        {
foreach (var responseItem in responseWithResults.Responses)
                            {
                                if (responseItem.Response != null) { }
                                if (responseItem.Fault != null) { }                        
                            }
                        }

No comments:

Post a Comment

Day 11 - Customizing Option Sets Dropdown Fields with JavaScript

  In this Blog, we will see how to Customizing Option Sets Dropdown Fields with JavaScript label1 = formContext.getAttribute( "bosch_op...