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
Uma PrabhakarUma Prabhakar 

How to Increment and decrement a Field value based on the Record addition and Record Deletion using trigger

i have written a increment trigger, where there are 5 custom field(India,ME,US,SEA,EU),all these fields are Incremented based on a formula field(PraticeLWC__Region__c), now if i update for ME the record should increment for 1 and 2, and if i delete a record related to ME and again add the record the record for ME it should take as 2 itself and not 3, how do i achive this

so basically when i add the records based on the region it should get incremented and if i delete a record it should get decremented

Region1
 
trigger Increment on Lead (before insert,Before Update) {
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate || Trigger.IsUnDelete))  {
        List<Lead> leadList = [Select Id,PraticeLWC__Lead_No_Region_IN__c From Lead Where PraticeLWC__Region__c = 'India'];
        For(Lead l : trigger.New ) {
            if(l.PraticeLWC__Region__c == 'India') {
                if(leadList.size() > 0){
                    l.PraticeLWC__Lead_No_Region_IN__c = leadList.size()+0;   
                } else {
                    l.PraticeLWC__Lead_No_Region_IN__c = 1;
                }
            }
        }
        List<Lead> leadListt = [Select Id,PraticeLWC__Lead_No_Region_USA__c From Lead Where PraticeLWC__Region__c = 'US'];
        For(Lead m : trigger.New) {
            if(m.PraticeLWC__Region__c == 'US') {
                if(leadListt.size() > 0){
                    m.PraticeLWC__Lead_No_Region_USA__c = leadListt.size()+0;   
                } else {
                    m.PraticeLWC__Lead_No_Region_USA__c = 1;
                }
            }
        } 
        List<Lead> leadListm = [Select Id,PraticeLWC__Lead_No_Region_EU__c From Lead Where PraticeLWC__Region__c = 'EU'];
        For(Lead n : trigger.New) {
            if(n.PraticeLWC__Region__c == 'EU') {
                if(leadListm.size() > 0){
                    n.PraticeLWC__Lead_No_Region_EU__c = leadListm.size()+0;   
                } else {
                    n.PraticeLWC__Lead_No_Region_EU__c = 1;
                }
            }
        } 
        List<Lead> leadListo = [Select Id,PraticeLWC__Lead_No_Region_SEA__c From Lead Where PraticeLWC__Region__c = 'SEA'];
        For(Lead o : trigger.New) {
            if(o.PraticeLWC__Region__c == 'SEA') {
                if(leadListo.size() > 0){
                    o.PraticeLWC__Lead_No_Region_SEA__c = leadListo.size()+0;   
                } else {
                    o.PraticeLWC__Lead_No_Region_SEA__c = 1;
                }
            }
        }
        List<Lead> leadListp = [Select Id,PraticeLWC__Lead_No_Region_ME__c From Lead Where PraticeLWC__Region__c = 'ME'];
        For(Lead p : trigger.New) {
            if(p.PraticeLWC__Region__c == 'ME') {
                if(leadListp.size() > 0){
                    p.PraticeLWC__Lead_No_Region_ME__c = leadListp.size()+0;   
                } else {
                    p.PraticeLWC__Lead_No_Region_ME__c = 1;
                }
            }
        } 
        
    }                   
}

 
SRKSRK

1) try never put formula fields in where clause (there is a governer limit issue in salesforce for that, once number of record increse in a object more then 50000 this kind of quryes will fail)

 

2) now let me understand it correctly so you have 5 field on lead object named as  (India,ME,US,SEA,EU) and i assume there is child record which get inserted under a lead so when this child record get inserted you want to check the region on this child record and update the counter for the region on lead record accordingly ??

 

3) Or is it case that "PraticeLWC__Region__c " is a field on lead object and there are 5 fields on ead object named as  (India,ME,US,SEA,EU)  now if user save a lead record where "PraticeLWC__Region__c " = "india" then you want value of "India" field to increse by 1 and then if user update the lead record and chagne the country which change the  "PraticeLWC__Region__c" value so you want to increse that region field by 1 ?