You need to sign in to do that
Don't have an account?
Nirupama 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
Test Class
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; } }
You can update the code similar to this
Regards,
Naga
the Fields are not updating??
i tried to modify further, i am not able to update the fields
I updated the code to use the correct field names. I also tested it in my developer sandbox and it is working fine.
Let me know if you have any questions.
Regards,
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??
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.
i also did the same and i am getting very differently
Can you show me the code that you are using? Are there any other triggers on Lead?
i have not made any changes, See this is the below code