Home  >  Blog  >   Salesforce

Bulkification in Salesforce

Rating: 4.8
  
 
5376
  1. Share:
Salesforce Articles

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.

Bulkification in Salesforce - Table of Content

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;

} } } }

Sample Program for Bulkification: Using StartTest() or StopTest()

@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]

RealTimeProcess use case of code for Bulkification

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.

MindMajix YouTube Channel

[Visit here to learn Salesforce Training in Bangalore]

Sample Program #1

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’;
}	
}

Sample Program #2

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 ]

Sample Program #3

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 ]

Sample Program #4

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.

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience in Salesforce concepts

Salesforce Administration TrainingSalesforce Lightning Training
Salesforce Advanced Developer TrainingSalesforce Developer Training
Salesforce IoT TrainingSalesforce App Builder Certification Training
Salesforce AppExchange TrainingSalesforce Service Cloud Training
and many more. 
Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
Salesforce TrainingMay 25 to Jun 09View Details
Salesforce TrainingMay 28 to Jun 12View Details
Salesforce TrainingJun 01 to Jun 16View Details
Salesforce TrainingJun 04 to Jun 19View Details
Last updated: 03 Apr 2023
About Author

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.

read more
Recommended Courses

1 / 15