Bulk Operations using SDK.SOAP in Javascript

This is a second post of this series of blog posts, Previously we have described CRUD operations using SDK.SOAP.Js. In this blog post we would like to share details of Bulk Operations(Bulk Update, Bulk Delete) using SDK.SOAP.JS.  The following code snippets will show the details of these messages.

Bulk Update

  1. Include the “Sdk.Update.js” as web resource in the desired solution. Include the web resource in HTML document.
  2. Include the “Sdk.ExecuteMultiple.js” as web resource in the desired solution. Include the web resource in HTML document.
  3. Instantiate the entities that need to be updated, using
     var testAccount = new Sdk.Entity("Entity logical name"); 

    This function will get one parameter i.e. the logical name of entity to be updated.

    In our case we have instantiated three account entities.

  4. Add the new values of fields using
     addAttribute(new Sdk.String("", "")); 

    The first parameter will be field logical name and second value will be record value to be updated.

    In our case we will update name and phone number.

  5. Create and collection using Sdk.Collection() of Sdk.OrganizationRequest() type.
  6. Add newly instantiated account entities to the Sdk.Collection().
  7.  Now we need an execute multiple request to send these update requests to CRM.
  8. Instantiate execute multiple settings
     new Sdk.ExecuteMultipleSettings(true, true); 

    First parameter is for continue on error and second parameter is for returning responses.

  9. Than instantiate execute multiple request using
     new Sdk.ExecuteMultipleRequest(updateRequests, emSettings); 

    First parameter is of Sdk.Collection() type and second is of Sdk.ExecuteMultipleSettings().

  10. To get response from execute multiple request we need an object of type
     new Sdk.ExecuteMultipleResponseItem() 
  11. Now execute the ExecuteMultipleRequest in Async mode.

Complete function call to update entity


function BulkUpdateEntities() {
 //Intantiate Entites to be Updated
 var account1 = new Sdk.Entity("account");
 //Add attributes that need to be updated
 account1.addAttribute(new Sdk.Guid("accountid", "B25317D4-AE5F-E611-80EB-5065F38B05B1"));
 account1.addAttribute(new Sdk.String("name", "aa Test U"));
 account1.addAttribute(new Sdk.String("telephone1", "+111-222-333"));

 var account2 = new Sdk.Entity("account");
 account2.addAttribute(new Sdk.Guid("accountid", "B9F693DE-AE5F-E611-80EB-5065F38B05B1"));
 account2.addAttribute(new Sdk.String("name", "aa Test U1"));
 account2.addAttribute(new Sdk.String("telephone1", "+111-222-333"));

 var account3 = new Sdk.Entity("account");
 account3.addAttribute(new Sdk.Guid("accountid", "543C1A1E-875F-E611-80EB-5065F38B05B1"));
 account3.addAttribute(new Sdk.String("name", "aa Test U3"));
 account3.addAttribute(new Sdk.String("telephone1", "+111-222-333"));

 //Create Organiztion Service Request
 var updateRequests = new Sdk.Collection(Sdk.OrganizationRequest);

 //Add requests to the collection
 updateRequests.add(new Sdk.UpdateRequest(account1));
 updateRequests.add(new Sdk.UpdateRequest(account2));
 updateRequests.add(new Sdk.UpdateRequest(account3));

 //Instantiate Execute Multiple Settings
 var emSettings = new Sdk.ExecuteMultipleSettings(true, true);

 //Create ExecuteMultiple Service Request
 var req = new Sdk.ExecuteMultipleRequest(updateRequests, emSettings);

 //Instantiate Response Item to get Execute Multiple Request's Respose
 var res = new Sdk.ExecuteMultipleResponseItem();

 //Execute using Async Mode
 Sdk.Async.execute(req,
 function (res) {
 //Get Organization Respose
 for (var i = 0; i < res.getResponses().toArray().length; i++) {
 if (res.getResponses().toArray()[i].getFault() != null) {
 console.log(res.getResponses().toArray()[i].getFault().getMessage());
 }
 else {
 alert("Successfully Updated");
 }
 }

 },
 //Call back function for Error Handling
 function (error) {
 console.log(e.message);
 });
}

Bulk Delete

  1. Include the “Sdk.Delete.js” as web resource in the desired solution. Include the web resource in HTML document.
  2. Include the “Sdk.ExecuteMultiple.js” as web resource in the desired solution. Include the web resource in HTML document.
  3. Instantiate Entity References of the records that need to be deleted using

new Sdk.EntityReference()

First parameter will be the entity logical name and second parameter will be the GUID of record that need to be deleted.

  1. Create and collection using Sdk.Collection() of Sdk.OrganizationRequest() type.
  2. Add newly instantiated account entity refrences to the Sdk.Collection().
  3. Now we need an execute multiple request to send these delete requests to CRM.
  4. Instantiate execute multiple settings

new Sdk.ExecuteMultipleSettings(true, true);

First parameter is for continue on error and second parameter is for returning responses.

  1. Than instantiate execute multiple request using

new Sdk.ExecuteMultipleRequest(updateRequests, emSettings);

First parameter is of Sdk.Collection() type and second is of Sdk.ExecuteMultipleSettings().

  1. To get response from execute multiple request we need an object of type

new Sdk.ExecuteMultipleResponseItem()

  1. Now execute the ExecuteMultipleRequest in Async mode.
function BulkDeleteEntities() {
 //Intantiate Entity Refrences to be Deleted
 var account1 = new Sdk.EntityReference("account", "B25317D4-AE5F-E611-80EB-5065F38B05B1");
 var account2 = new Sdk.EntityReference("account", "D94AFC06-B05F-E611-80EB-5065F38B05B1");
 var account3 = new Sdk.EntityReference("account", "543C1A1E-875F-E611-80EB-5065F38B05B1");

 //Create Organiztion Service Request
 var delRequests = new Sdk.Collection(Sdk.OrganizationRequest);

 //Add requests to the collection
 delRequests.add(new Sdk.DeleteRequest(account1));
 delRequests.add(new Sdk.DeleteRequest(account2));
 delRequests.add(new Sdk.DeleteRequest(account3));

 //Instantiate Execute Multiple Settings
 var emSettings = new Sdk.ExecuteMultipleSettings(true, true);

 //Create ExecuteMultiple Service Request
 var req = new Sdk.ExecuteMultipleRequest(delRequests, emSettings);

 //Instantiate Response Item to get Execute Multiple Request's Respose
 var res = new Sdk.ExecuteMultipleResponseItem();

 //Execute using Async Mode
 Sdk.Async.execute(req,
 function (res) {
 //Get Organization Respose
 for (var i = 0; i < res.getResponses().toArray().length; i++) {
 if (res.getResponses().toArray()[i].getFault() != null) {
 console.log(res.getResponses().toArray()[i].getFault().getInnerFault());
 }
 else {
 console.log(res.getResponses().toArray()[i]);
 alert("Successfully Deleted");
 }
 }

 },
 //Call back function for Error Handling
 function (e) {
 console.log(e.message);
 });
}

In this blog post we have performed Bulk operations using SDK.SOap.JS. In the next post of this series we will describe how can we retrieve entity metadata and user privileges using SDK.SOAP messages.



1 Comment

Leave a Reply