In Salesforce, we always try to write code that is Bulkified. This is termed the ideal way to write code. Bulkified Code or Bulkification means combining the respective tasks in the APEX. It is the only way to get around Governor Limits.
Eg:
Public class JeanClassDemonstration{
Public static void applyDiscount(list<Levis__c>JeanListNew){
for(Levis__c j: JeanListNew){
if(j.Price__c >= 1000){
j.Price__c = j.Price__c - 100;
} } } }
@isTest // Called as Annotation
Public class JeanDiscountClassTest(
//we are calling MyTestFunction1(){} -------> 30 DML
//we are calling MyTestFunction2(){} -------> 40 DML
Static testmethod void MyTestFunction3(){
======some other code======
======some other code======
======some other code======
Test.StartTest(); ----------------------> we can have 150DML commands inside the Start & Stop of Test ();
//create a new record data
Levis__c j = new Levis__c();
j.Name = ‘Louis’;
j.Price__c = 200;
//insert jean
Insert j;
//trigger will come to picture
//retrive the new jean
Levis__c j2 = new Levis__c ();
//same like, list<> MyList = New List<string>();
j2=[SELECT Price__c FROM Levis__c WHERE id=j.id];
//test that the trigger correctly updated the price
system.assertEquals(900, j2.Price__c);
Test.StopTest(); ------------------------------------------------------>
}
======some other code======
======some other code======
======some other code======
//we are calling MyTestFunction4(){} ---------> 60 DML
//we are calling MyTestFunction5(){} ---------> 10 DML
In the above Bulkification sample program, we have a maximum number of DML queries allowed. In order to use a few more DML queries, the function “Test. start test(); & Test.StopTest();” is required. The DML queries in between these functions are counted from zero to 150 and permit to have the developer, more access to include DML queries in the function, which is useful in real-time processes.
Do you want to get certified and build your career in Salesforce? Then enroll in "Online Salesforce Certification Training" this course will help you to achieve excellence in this domain. |
[Leave an Inquiry to learn Salesforce Training in Hyderabad]
Note: As mentioned earlier, one should avoid business logic in Trigger, and still we are doing this in the below code just to avoid length and focus more on the Bulkification concept.
[Visit here to learn Salesforce Training in Bangalore]
Trigger Code: Code which will work on one record or ‘n’ number of records for Bulkification.
Wrong Code:
Trigger ChangeIndustry on Account (Before Insert){
Account.a = trigger.New[0];
a.Industry = ‘IT’;
}
Correct Code:
Trigger ChangeIndustry on Account (Before Insert){
for(Account.a = trigger.New){
a.Industry = ‘IT’;
}
}
Wrong Code:
Trigger DemoTrigger on Levis__c (Before Insert){
list<JeanProductDetails> QueryResult = New list<JeanProductDetails>();
for(Levis__c j : trigger.New){
QueryResult = [SELECT productname__c FROM JeanProductDetails__c WHERE Price__c IN : AllPriceList ];
//and some business logic on records.
}
}
Note:
Comparing with field name or variable → =: (Age = : Age_Of_Ajay__c)
Comparing with hard coded data → = (Eg: Age = 20)
Correct Code:
Trigger DemoTrigger on Levis__c (Before Insert){
//code for getting all price values
list<integer> AllPriceList = New List<integer>();
//Hint: whatever value we are comparing in the query, just collect those values in a list, using for each loop, and do nothing else in loop
for(Levis__c j : trigger.New){
AllPriceList.add(j.Price__c);
//remove query from loop
}
list<JeanProductDetails__c> QueryResult = [SELECT id, productname__c FROM JeanProductDetails__c WHERE Price__c IN : AllPriceList ];
//IN - (1000,1200,300 )
For (JeanProductDetails__c j : Query Result){
// and some business logic on product records using loop
} }
Note:
=: → for comparing with one value
IN: → for comparing with multiple values/means, List.
[ Related Article: Salesforce Interview Questions ]
Wrong Code:
Public Class CompanyDetails{
Public static void SearchIndustry (like<> CompanyList){
for(Company__c.p : CompanyList ){
Account a = [SELECT industry FROM Account WHERE name= : p.name__c];
//some business logic
} } }
Correct Code:
Public Class CompanyDetails{
Public static void SearchIndustry (like<> CompanyList){
list<string> CompanyNameList = New List<string>();
for(Company__c p : CompanyList){
CompanyNameList.add(p.Name__c);
//IBM, Oracle, Google
}
list<> AccList = [SELECT industry FROM Account WHERE name IN : CompanyNameList];
for(Account a : AccList){
//some business logic on AccList using for each
} } }
[ Related Article: Salesforce Tutorial ]
Wrong Code:
Trigger DemoTrigger on DemoObject (Before Insert){
list<DemoObject>QueryResult=[SELECT productname FROM DemoObject];
for(DemoObject d : QueryResult){
DemoObject2 obj = new DemoObject2();
obj.Price = d.Price;
Insert obj;
} } }
Correct Code:
Trigger DemoTrigger on DemoObject (Before Insert){
list<> QueryResult = [SELECT productName FROM DemoObject ];
list<> ListForBulkDML = New list<Levis__c>();
for(DemoObject d : QueryResult ){
Levis__c obj = new Levis__c();
obj.Price = d.Price;
ListForBulkDML.add(obj);
}
if(ListForBulkDML.IsEmpty() == false){ → //Best Practice
Insert ListForBulkDML;
} }
In the next topic, we will discuss in detail “Standard Controller In Salesforce”. Keep following us for more info on Salesforce Development / Programming.
Mindmajix offers different Salesforce certification training according to your desire with hands-on experience in Salesforce concepts
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Salesforce Training | Dec 24 to Jan 08 | View Details |
Salesforce Training | Dec 28 to Jan 12 | View Details |
Salesforce Training | Dec 31 to Jan 15 | View Details |
Salesforce Training | Jan 04 to Jan 19 | View Details |
Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.