Bulk State Transition

Author - Webner
21.12.2021
|
0 Comments
||

The Bulk State Design pattern is the general pattern that executes the bulk of the actions in Apex based on the change of state of one or more records. Bulk transitions increase the code reusability by allowing only records that fulfill the requirements to be processed.

Design Patterns for Bulk Triggers:

Apex triggers are optimized to work in bulk. When using bulk design triggers, you have a lot of options.

  • Better performance
  • Use fewer server resources.
  • Governor Limits

The benefits of bulking your code are that you can access a large number of records efficiently and run within the governor limit of the lightning platform. The governor limit ensures that the runway code does not affect the multi-tenant platform.
In the Bulk State Transition Design pattern in Triggers,

  • The trigger looks for records that have changed state and are eligible to be triggered.
  • It only passes the eligible records when a call is made to the utility class to do the work.
  • The method within the utility class performs the operation in bulk.

Performing Bulk Trigger:

In this example, the utility class receives the records that meet the criterion from the trigger. Trigger events and also the DML type are used to frame the trigger. The utility class is reusable within the future. The subsequent example shows the majority of bulk state transition.

Apex Trigger
trigger PolicyAccountTrigger on Policy__c (after insert, after update) {
List<Account> acctUpdate = new List<Account>();
for (Policy__c pol : Trigger.New) {
if (Trigger.isInsert && pol.status__c == 'Renew') {
Account acctData = [select Id, Name, No_of_Renew_Policies__c from Account where Id=:pol.Account_ID__c ];
acctUpdate.add(acctData);
} else if (Trigger.isUpdate && pol.status__c == 'Renew') {
Account acctData = [select Id, Name, No_of_Renew_Policies__c from Account where Id=:pol.Account_ID__c ];
acctUpdate.add(acctData);
}
}
if (!acctUpdate.isEmpty()) {
updatePolicyAccount policy = new updatePolicyAccount();
policy.policyStatusAccChange(acctUpdate);
}
}

Apex Class

public class updatePolicyAccount {
public void policyStatusAccChange(List<Account> acctUpdate) {
for (Account acct : acctUpdate) {
Decimal PolicyNo = acct.No_of_Renew_Policies__c + 1;
acct.No_of_Renew_Policies__c = PolicyNo;
}
Update acctUpdate;
}
}

The bulk state transition necessitates the use of a trigger that allows just those records that have changed state to be affected, as well as a utility class, to implement the logic. When utilized for bulk transactions, the code does not exceed the DML governor limit per transaction. The utility class is used in the trigger, which allows the code to be reused.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published.