Bulkification in Salesforce

In Salesforce, we always attempt to write a code which is Bulkified. This is termed as the ideal way to write code. Bulkified Code or Bulkification means combining the respective tasks within the APEX. It is the sole thanks to get around Governor Limits.

Check it out more info at salesforce online training
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();
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’ve maximum 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.
RealTimeProcess use case of code for Bulkification:
Note: As mentioned earlier, one should avoid business logic in Trigger, and still we do that in below code just to avoid length and focus more on Bulkification concept.

check out Salesforce Interview questions
Sample Program#1
Trigger Code: Code which can 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 AllPriceList = New List();

//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.

Check it out SalesForce tutorial
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 CompanyNameList = New List();

	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
}
}
}
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 about “Standard Controller In SOSQL Salesforce”. Keep following us for more info on Salesforce Development / Programming.

#salesforce #salesforcelogin #salesforcecrm #salesforcecpq

Bulkification in Salesforce
6.25 GEEK