Monday, May 2, 2016

Bulk Update Records in CRM 2015

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

                UpdateRequest updateRequest = new UpdateRequest { Target = getAllrecords };

                requestWithResults.Requests.Add(updateRequest);
            }

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

How to Import Data from Excel to Multiple tables in PowerApps

Introduction:   I have created a view on how to import data from excel to multiple tables in Canvas PowerApps   Implemented Steps: ...