Salesforce — Enable Mass Quick Action
By: Hariprasath Thanarajah
Tags: Salesforce, Quick Action

I am going to explain the functionality of Salesforce ‘Mass Quick Action’ with the help of a real use-case.

Scenario

The scenario is to generate a pending request record to a Contact object in custom functionality. (The Request record will be generated with different statuses for Example ‘Pending’, ‘Failed’, ‘Refused’ etc).

Here we need to generate the pending request records for Contacts by selecting multiple Contacts(It can be 1 or 10 or 100).

Solution 1

Since lightning is recommended by Salesforce we can create a custom lightning component.

Query all the Contacts. Select multiple contacts(1 or 10 or 100 but need to manually select). Click a button to generate the pending request records for those selected Contacts.

Limitations and the Issues on Solution 1

If you have millions of Contact? Will Solution 1 is good?

No, Because we don’t have any filter condition to show a specific number of Contacts. Then?

We can simply ask the user to give the filter condition? damn. will all the user/ admin know how to put filter condition? which means do they familiar with the SOQL?

No. Then?

Solution 2

We have an inbuilt functionality called Mass quick action in Salesforce can be added to an object’s search layout.

After setup a mass quick action, we can select up to 100 records in a list view and perform mass updates.

You can use a mass quick action with cases, leads, accounts, campaigns, contacts, opportunities, work orders, and custom objects that support quick actions and have a search layout in Lightning Experience.

You can’t perform mass quick actions on a Recently Viewed list so you have to create your own list view to use this Solution.

How can we Implement Solution 2 for our need?

Create a checkbox field in Contact object called Is_GDPR_Request_Generation__c with unchecked as default value.

Create a quick action in Contact object as ‘Generate Request’ by following,

Salesforce lightning experience -> Object Manager -> Contact -> Buttons, Links and Actions -> New Action -> Fill the form like below -> Save

Quick action for Mass Update

When you click Save button you will be redirected to a page to set up the quick action layout. Remove all the fields from the layout and Save -> Yes. You will be redirected to the following page.

Click New button at the section Predefined Field Values,

Select the field which you created in Contact and give the default value is true.

Click search layout of Contact and edit the list view -> Under List View Actions in Lightning Experience section -> add the Generate Request quick action to Selected Quick Actions tab.

Create a list view on Contact object(Refer https://help.salesforce.com/articleView?id=customviews.htm&type=5),

The list view for the contact object you updated now displays buttons for the actions you added.

The Users can select multiple Contact records in the newly created list view and use the mass quick action button to perform bulk updates.

How can we update the Contact record and generate Request record?

Create a Trigger in the Contact after an update like below.

trigger ContactTrigger on Contact (after insert, after update) { if(Trigger.isAfter) {

if(Trigger.isUpdate && UTILS.stopRequestGeneration == false) {

UTILS.stopRequestGeneration = true;

List<Id> contactsIdToGenerateRequest = new List<Id>();



for(Contact con : Trigger.new) {

contactsIdToGenerateRequest.add(con.Id);

}

UTILS.generateRequestForSelectedContact(contactsIdToGenerateRequest);

}

}

} -------------------------------------------------------------- public with sharing class UTILS{ public Static Boolean stopRequestGeneration {

get{

if (stopRequestGeneration == null){

stopRequestGeneration = false;

}

return stopRequestGeneration;

}

set;

} public void generateRequestForSelectedContact(List<Id> contactIds) {



//Query the Contacts with its Requests where it status is pending.

List<Contact> contactList = [SELECT Id, (SELECT Id, Status__c FROM Requests__r WHERE Status__c = 'Pending') FROM Contact WHERE Id IN :contactIds];



List<Contact> selectedContactsToGenerateRequest = new List<Contact>();

List<Contact> allContacts = new List<Contact>();

List<Contact> allContactsToUpdate = new List<Contact>();



//loop through the queried Contact and if it is not have Requests with pending status fill to selectedContactsToGenerateRequest and also to update Contacts Is_GDPR_Request_Generation__c field fill all the Contact into allContacts.

for(Contact con: contactList) {

if(con.Requests__r.size() == 0) {

selectedContactsToGenerateRequest.add(con);

}

allContacts.add(con);

}



List<Request__c> requestsLst = new List<Request__c>();



//Create Requests for the valid Contacts

if(selectedContactsToGenerateRequest.size() > 0) {

for (Contact contact : selectedContactsToGenerateRequest) {

Request__c request = new Request__c(

Contact__c = contact.Id, Status__c = 'Pending', Type__c = 'Test'

);

gequestsLst.add(request);

}



if(requestsLst.size() > 0) {

insert requestsLst;

}

}



//Update the Contacts Is_GDPR_Request_Generation__c to false.

if(allContacts.size() > 0) {

for (Contact contact : allContacts) {

contact.Is_GDPR_Request_Generation__c = false;

allContactsToUpdate.add(contact);

}



if(allContactsToUpdate.size() > 0) {

update allContactsToUpdate;

}

}

}

}

Summary

Create a Boolean field at Contact. Create a quick action at Contact and make the predefined value is true when you select multiple contacts to update via the quick action. Create a trigger at Contact to call the specific logic to generate a request for those Contact when the Contact Boolean value is true. Generate the requests and change back the Contact’s Boolean value to false if we need to generate the Requests again to that particular Contacts.

References:

The Considerations when we play with quick action can be found,

https://help.salesforce.com/articleView?id=customviews.htm&type=5