Bulk Edit and Restriction of bulk edit in Microsoft Dynamics CRM

Hi all,

This post may be very old on bulk edit feautre in Microsoft Dynamics CRM but hope this gives some more information on bulk edit.

Bulk Edit in Microsoft Dynamics CRM:

Editing record is very easy in Microsoft Dynamics CRM.But what if we need to edit a field of all records with same value. So here, Instead of opening records one by one and editing it, we can also use Bulk edit feature which edits that field in one go for all records.

Limitations:

Not so good for large numbers of customer records where results ‘go onto the next page’ of the results grid – you can only select one page of results at a time so you would need to repeat the bulk edit steps.

By default the scripts are disabled for bulk edit forms.But we can enable on load and on change event scripts for bulk edit.

On save scripts will not be triggered while performing Bulk Edit

Restriction of Bulk Edit for particular records:

Because of its limitations thought of Exploring a bit more on bulk edit with a requirement  restricting bulk edit of different owner records.

Steps:

  1. Created a custom entity .Bulk edit previllege will not be available for users unless miscelleaneous previllege Bulk Edit is enabled.Capture
  2. Inorder to restrict bulk edit for particular records , create a enable rule(custom javascript rule) for this button and add a parameter “CRM Parameter” for this enable rule and the name of this parameter “selectedcontrolselecteditemreferences“.
  3. This will retrieve all the selected records by the user for bulk edit

Javascript code:

function run(selectedItems)
{
    
    var selectedItem = selectedItems[0];
    alert(“Id=” + selectedItem.Id + “\nName=” + selectedItem.Name + “\nTypeCode=” + selectedItem.TypeCode.toString() + “\nTypeName=” + selectedItem.TypeName);
}
Here
selectedItem.Id — Selected record Guid
selectedItem.Name — Selected record Name
selectedItem.TypeCode — Entity Type code 
selectedItem.TypeName — Entity schema Name
By using record details we can perform our required actions on bulk edit . We can hide/show edit button based on our requirement .
Javascript for hiding a ribbon button :
function hideRibbonButton()
{
if(selectedItem!=null)
{
return true; // Shows button
}
else
{
return false; // Hides button
}
}
Enabling javascripts on Bulk Edit:

By default the scripts are disabled for bulk edit forms.

To enable the script for Bulk edit forms check the below syntax

<event
active=[“0” “1” “true” “false”]application=[“0” “1” “true” “false”]attribute=”String”
BehaviorInBulkEditForm=”String”
eventType=[“DataEvent” “ControlEvent”]name=”String”></event

Export the entity as solution from CRM system by adding the required entity where bulk edit need to be enabled along with javascript. By default server side events gets enabled for bulk edit. But inorder to enable client side events, open the file  called Customizations.xml from downloaded solution and find  <formlibraries>.

Capture

Note: Microsoft Dynamics CRM provides multi forms and you can enable for single form or multiple forms.

Save and Import the solution, publish it. Now javascript gets enabled on the solution

Performing Bulk Create,Update,Delete by using C# ExecuteMultipleRequest:

Bulk Create:

 

/// <summary>
   /// Call this method for bulk Create
   /// </summary>
   /// <param name="service">Org Service</param>
   /// <param name="entities">Collection of entities to Create</param>
   public static void BulkCreate(IOrganizationService service, DataCollection<Entity> entities)
   {
       // Create an ExecuteMultipleRequest object.
       var multipleRequest = new ExecuteMultipleRequest()
       {
           // Assign settings that define execution behavior: continue on error, return responses.
           Settings = new ExecuteMultipleSettings()
           {
               ContinueOnError = false,
               ReturnResponses = true
           },
           // Create an empty organization request collection.
           Requests = new OrganizationRequestCollection()
       };
 
       // Add a CreateRequest for each entity to the request collection.
       foreach (var entity in entities)
       {
           CreateRequest createRequest = new CreateRequest { Target = entity };
           multipleRequest.Requests.Add(createRequest);
       }
 
       // Execute all the requests in the request collection using a single web method call.
       ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest);
 
   }

 

Bulk Update:

/// <summary>
   /// Call this method for bulk update
   /// </summary>
   /// <param name="service">Org Service</param>
   /// <param name="entities">Collection of entities to Update</param>
   public static void BulkUpdate(IOrganizationService service, DataCollection<Entity> entities)
   {
       // Create an ExecuteMultipleRequest object.
       var multipleRequest = new ExecuteMultipleRequest()
       {
           // Assign settings that define execution behavior: continue on error, return responses.
           Settings = new ExecuteMultipleSettings()
           {
               ContinueOnError = false,
               ReturnResponses = true
           },
           // Create an empty organization request collection.
           Requests = new OrganizationRequestCollection()
       };
 
       // Add a UpdateRequest for each entity to the request collection.
       foreach (var entity in entities)
       {
           UpdateRequest updateRequest = new UpdateRequest { Target = entity };
           multipleRequest.Requests.Add(updateRequest);
       }
 
       // Execute all the requests in the request collection using a single web method call.
       ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest);
 
   }

 

Bulk Delete:

/// <summary>
   /// Call this method for bulk delete
   /// </summary>
   /// <param name="service">Org Service</param>
   /// <param name="entityReferences">Collection of EntityReferences to Delete</param>
   public static void BulkDelete(IOrganizationService service, DataCollection<EntityReference> entityReferences)
   {
       // Create an ExecuteMultipleRequest object.
       var multipleRequest = new ExecuteMultipleRequest()
       {
           // Assign settings that define execution behavior: continue on error, return responses.
           Settings = new ExecuteMultipleSettings()
           {
               ContinueOnError = false,
               ReturnResponses = true
           },
           // Create an empty organization request collection.
           Requests = new OrganizationRequestCollection()
       };
 
       // Add a DeleteRequest for each entity to the request collection.
       foreach (var entityRef in entityReferences)
       {
           DeleteRequest deleteRequest = new DeleteRequest { Target = entityRef };
           multipleRequest.Requests.Add(deleteRequest);
       }
 
       // Execute all the requests in the request collection using a single web method call.
       ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest);
   }

Hope this helps 🙂

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s