function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nirupama SharmaNirupama Sharma 

How to bulkify the code for the below trigger

Hello everyone

I need a small help in bulkifying the below code, Basically what this trigger does is when country is populated, it checks with the Formula Field (Updated_Region__c) and assigns with the Correct Region Field, Ex - when Updated Region is Middle east(ME) then the custom fields related to ME(Lead_No_Region_ME__c) gets incremented with 1,2,3....

Now i hear this is not a best pratices for deploying in the production, because when bulk records gets updated, the code may fail, but to my suprise i have Written a Test class with code coverage of 98%, Could any please help with the solution or suggestion
 
trigger LeadIncrement on Lead (before insert,Before Update) {
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
        List<Lead> leadList = [Select Id,Lead_No_Region_IN__c From Lead Where Region__c = 'India'];
        For(Lead l : trigger.New) {
            if(l.Region__c == 'India') {
                if(leadList.size() > 0){
                    l.Lead_No_Region_IN__c = leadList.size()+0;   
                } else {
                    l.Lead_No_Region_IN__c = 1;
                }
            }
        }
        List<Lead> leadListt = [Select Id,Lead_No_Region_USA__c From Lead Where Region__c = 'US'];
        For(Lead m : trigger.New) {
            if(m.Region__c == 'US') {
                if(leadListt.size() > 0){
                    m.Lead_No_Region_USA__c = leadListt.size()+0;   
                } else {
                    m.Lead_No_Region_USA__c = 1;
                }
            }
        } 
        List<Lead> leadListm = [Select Id,Lead_No_Region_EU__c From Lead Where Region__c = 'EU'];
        For(Lead n : trigger.New) {
            if(n.Region__c == 'EU') {
                if(leadListm.size() > 0){
                    n.Lead_No_Region_EU__c = leadListm.size()+0;   
                } else {
                    n.Lead_No_Region_EU__c = 1;
                }
            }
        } 
        List<Lead> leadListo = [Select Id,Lead_No_Region_EU__c From Lead Where Region__c = 'SEA'];
        For(Lead o : trigger.New) {
            if(o.Region__c == 'SEA') {
                if(leadListo.size() > 0){
                    o.Lead_No_Region_SEA__c = leadListo.size()+0;   
                } else {
                    o.Lead_No_Region_SEA__c = 1;
                }
            }
        }
        List<Lead> leadListp = [Select Id,Lead_No_Region_ME__c From Lead Where Region__c = 'ME'];
        For(Lead p : trigger.New) {
            if(p.Region__c == 'ME') {
                if(leadListp.size() > 0){
                    p.Lead_No_Region_ME__c = leadListp.size()+0;   
                } else {
                    p.Lead_No_Region_ME__c = 1;
                }
            }
        } 
        
    }					
}

Test Class
 
@isTest
public class LeadIncrementV3Test {
    @isTest
    public static void LeadIncrementV3TestMethod() {
        List<Lead> leadList = new List<Lead>();

        Lead l = new Lead();
        l.Country = 'India';
        l.Company = 'Test';
        l.LastName = 'testLast';
        l.Status = 'Enquiry';
        leadList.add(l);

        Lead l1 = new Lead();
        l1.Country = 'India';
        l1.Company = 'Test1';
        l1.LastName = 'testLast1';
        l1.Status = 'Enquiry';
        leadList.add(l1);

        Lead l2 = new Lead();
        l2.Country = 'US';
        l2.Company = 'Test2';
        l2.LastName = 'testLast2';
        l2.Status = 'Enquiry';
        leadList.add(l2);

        Lead l21 = new Lead();
        l21.Country = 'USA';
        l21.Company = 'Test21';
        l21.LastName = 'testLast21';
        l21.Status = 'Enquiry';
        leadList.add(l21);

        Lead l3 = new Lead();
        l3.Country = 'Brazil';
        l3.Company = 'Test3';
        l3.LastName = 'testLast3';
        l3.Status = 'Enquiry';
        leadList.add(l3);

        Lead l31 = new Lead();
        l31.Country = 'Belgium';
        l31.Company = 'Test31';
        l31.LastName = 'testLast31';
        l31.Status = 'Enquiry';
        leadList.add(l31);

        Lead l4 = new Lead();
        l4.Country = 'Thailand';
        l4.Company = 'Test4';
        l4.LastName = 'testLast4';
        l4.Status = 'Enquiry';
        leadList.add(l4);

        Lead l41 = new Lead();
        l41.Country = 'Japan';
        l41.Company = 'Test41';
        l41.LastName = 'testLast41';
        l41.Status = 'Enquiry';
        leadList.add(l41);

        Lead l5 = new Lead();
        l5.Country = 'Iran';
        l5.Company = 'Test5';
        l5.LastName = 'testLast5';
        l5.Status = 'Enquiry';
        leadList.add(l5);

        Lead l51 = new Lead();
        l51.Country = 'Kuwait';
        l51.Company = 'Test51';
        l51.LastName = 'testLast51';
        l51.Status = 'Enquiry';
        leadList.add(l51);  
        
        Insert leadList;
    }
}

​​​​​​​
Naga  AlapatiNaga Alapati
Hi Shravan,

You can update the code similar to this
 
trigger LeadIncrement on Lead (before insert,Before Update) {
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
        List<Lead> leadList = [Select Id, Region__c From Lead];
        Map<String, List<Lead>> countryLeadsMap = new Map<String, List<Lead>>();
        
        for (Lead l : leadList) {
            if (!countryLeadsMap.containsKey(l.Region__c)) {
                countryLeadsMap.put(l.Region__c, new List<Lead>());
            }
            countryLeadsMap.get(l.Region__c).add(l);
        }
        
        for (Lead nl : trigger.New) {
            countryLeadsMap.get(l.Region__c).add(nl);
            if (l.Region__c == 'India') {
                l.Lead_No_Region_IN__c = countryLeadsMap.get(l.Region__c).size();
            }

            if (l.Region__c == 'US') {
                l.Lead_No_Region_IN__c = countryLeadsMap.get(l.Region__c).size();
            }

            if (l.Region__c == 'EU') {
                l.Lead_No_Region_IN__c = countryLeadsMap.get(l.Region__c).size();
            }

            if (l.Region__c == 'SEA') {
                l.Lead_No_Region_IN__c = countryLeadsMap.get(l.Region__c).size();
            }

            if (l.Region__c == 'ME') {
                l.Lead_No_Region_IN__c = countryLeadsMap.get(l.Region__c).size();
            }
        }
    }
}

Regards,
Naga​​​​​​​
Nirupama SharmaNirupama Sharma
@Naga

the Fields are not updating??

i tried to modify further, i am not able to update the fields
 
trigger LeadIncrement on Lead (before insert,Before Update) {
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
        List<Lead> leadList = [Select Id,PraticeLWC__Lead_No_Region_IN__c,PraticeLWC__Lead_No_Region_USA__c,PraticeLWC__Lead_No_Region_EU__c, PraticeLWC__Region__c,PraticeLWC__Lead_No_Region_SEA__c,PraticeLWC__Lead_No_Region_ME__c From Lead WHERE PraticeLWC__Region__c = 'India' OR PraticeLWC__Region__c = 'US' OR  PraticeLWC__Region__c = 'EU' OR PraticeLWC__Region__c = 'SEA' OR PraticeLWC__Region__c = 'ME'];
        Map<String, List<Lead>> countryLeadsMap = new Map<String, List<Lead>>();
        
        for (Lead l : leadList) {
            if (!countryLeadsMap.containsKey(l.PraticeLWC__Region__c)) {
                countryLeadsMap.put(l.PraticeLWC__Region__c, new List<Lead>());
            }
            countryLeadsMap.get(l.PraticeLWC__Region__c).add(l);
        }
        
        for (Lead nl : leadList) {
            countryLeadsMap.get(nl.PraticeLWC__Region__c).add(nl);
            if (nl.PraticeLWC__Region__c == 'India') {
                nl.PraticeLWC__Lead_No_Region_IN__c = countryLeadsMap.get(nl.PraticeLWC__Region__c).size();
            }
            
            for (Lead ml : leadList) {
                countryLeadsMap.get(ml.PraticeLWC__Region__c).add(ml);
                if (ml.PraticeLWC__Region__c == 'US') {
                    ml.PraticeLWC__Lead_No_Region_USA__c = countryLeadsMap.get(ml.PraticeLWC__Region__c).size();
                }
                
                for (Lead ol : leadList) {
                    countryLeadsMap.get(ol.PraticeLWC__Region__c).add(ol);
                    if (ol.PraticeLWC__Region__c == 'EU') {
                        ol.PraticeLWC__Lead_No_Region_EU__c = countryLeadsMap.get(ol.PraticeLWC__Region__c).size();
                    }
                    for (Lead pl : leadList) {
                        countryLeadsMap.get(pl.PraticeLWC__Region__c).add(pl);
                        if (pl.PraticeLWC__Region__c == 'SEA') {
                            pl.PraticeLWC__Lead_No_Region_SEA__c = countryLeadsMap.get(pl.PraticeLWC__Region__c).size();
                        }
                        for (Lead ql : leadList) {
                            countryLeadsMap.get(ql.PraticeLWC__Region__c).add(ql);
                            if (ql.PraticeLWC__Region__c == 'ME') {
                                ql.PraticeLWC__Lead_No_Region_ME__c = countryLeadsMap.get(ql.PraticeLWC__Region__c).size();
                            }
                        }
                    }
                }
            }
        }
    }
}

 
Naga  AlapatiNaga Alapati
Hi Shravan,

I updated the code to use the correct field names. I also tested it in my developer sandbox and it is working fine.
 
trigger LeadIncrement on Lead (before insert,Before Update) {
	if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
		List<Lead> leadList = [Select Id, Region__c From Lead];
		Map<String, List<Lead>> countryLeadsMap = new Map<String, List<Lead>>();

		for (Lead l : leadList) {
			if (!countryLeadsMap.containsKey(l.Region__c)) {
				countryLeadsMap.put(l.Region__c, new List<Lead>());
			}
			countryLeadsMap.get(l.Region__c).add(l);
		}

        for (Lead nl : trigger.New) {
            if (!countryLeadsMap.containsKey(nl.Region__c)) {
                countryLeadsMap.put(nl.Region__c, new List<Lead>());
            }
			countryLeadsMap.get(nl.Region__c).add(nl);
			if (nl.Region__c == 'India') {
				nl.Lead_No_Region_IN__c = countryLeadsMap.get(nl.Region__c).size();
			}

			if (nl.Region__c == 'US') {
				nl.Lead_No_Region_USA__c = countryLeadsMap.get(nl.Region__c).size();
			}

			if (nl.Region__c == 'EU') {
				nl.Lead_No_Region_EU__c = countryLeadsMap.get(nl.Region__c).size();
			}

			if (nl.Region__c == 'SEA') {
				nl.Lead_No_Region_SEA__c = countryLeadsMap.get(nl.Region__c).size();
			}

			if (nl.Region__c == 'ME') {
				nl.Lead_No_Region_ME__c = countryLeadsMap.get(nl.Region__c).size();
			}
		}
	}
}

User-added image

Let me know if you have any questions.

Regards,
Naga
Nirupama SharmaNirupama Sharma
@Naga

the trigger is working fine, but when i try to insert 20 records of country india from data import wizard, all the 20 recoed Fields are getting updated as 20, and not 1,2,3....
How do i fix that??
Naga  AlapatiNaga Alapati
Hi Shravan,

I used the same code that i posted above and imported some test records through data import wizard in my developer org. I'm seeing the correct results. Can you check once.
User-added image
Nirupama SharmaNirupama Sharma
@Naga

i also did the same and i am getting very differently

User-added image
Naga  AlapatiNaga Alapati
Shravan,

Can you show me the code that you are using? Are there any other triggers on Lead?
Nirupama SharmaNirupama Sharma
@Naga

i have not made any changes, See this is the below code
 
trigger LeadIncrement on Lead (before insert,Before Update) {
	if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
		List<Lead> leadList = [Select Id, PraticeLWC__Region__c From Lead];
		Map<String, List<Lead>> countryLeadsMap = new Map<String, List<Lead>>();

		for (Lead l : leadList) {
			if (!countryLeadsMap.containsKey(l.PraticeLWC__Region__c)) {
				countryLeadsMap.put(l.PraticeLWC__Region__c, new List<Lead>());
			}
			countryLeadsMap.get(l.PraticeLWC__Region__c).add(l);
		}

        for (Lead nl : trigger.New) {
            if (!countryLeadsMap.containsKey(nl.PraticeLWC__Region__c)) {
                countryLeadsMap.put(nl.PraticeLWC__Region__c, new List<Lead>());
            }
			countryLeadsMap.get(nl.PraticeLWC__Region__c).add(nl);
			if (nl.PraticeLWC__Region__c == 'India') {
				nl.PraticeLWC__Lead_No_Region_IN__c = countryLeadsMap.get(nl.PraticeLWC__Region__c).size();
			}

			if (nl.PraticeLWC__Region__c == 'US') {
				nl.PraticeLWC__Lead_No_Region_USA__c = countryLeadsMap.get(nl.PraticeLWC__Region__c).size();
			}

			if (nl.PraticeLWC__Region__c == 'EU') {
				nl.PraticeLWC__Lead_No_Region_EU__c = countryLeadsMap.get(nl.PraticeLWC__Region__c).size();
			}

			if (nl.PraticeLWC__Region__c == 'SEA') {
				nl.PraticeLWC__Lead_No_Region_SEA__c = countryLeadsMap.get(nl.PraticeLWC__Region__c).size();
			}

			if (nl.PraticeLWC__Region__c == 'ME') {
				nl.PraticeLWC__Lead_No_Region_ME__c = countryLeadsMap.get(nl.PraticeLWC__Region__c).size();
			}
		}
	}
}