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
sonam gupthasonam guptha 

trigger on contact

hi 

I have 2 fields on contact which is inc__c and dec__c
so when  i set the value on inc__c = 20, Dec__c = 0 should set to null
if i change the value on Dec__c = 0 to Dec__c = 1 
the inc__c  should decrement to inc__c = 19.

if add one more Dec__c =2
i.e. inc__c = 18

how can i acheive this,if its trigger please give me the sample code,i really appreciate your responses

Thanks
 
Rahul.MishraRahul.Mishra
Hi, please try below code and let me know if it helps.

trigger incAndDec on Contact (before insert, before update) {
   
    for(Contact con : trigger.new) {          
        Decimal dec =  con.dec__c;
        Decimal inc =  con.inc__c;
        
        if(trigger.isInsert) {  
            if(dec != Null)
                con.inc__c = 20-dec ;
            
            if(inc!= Null)
                con.dec__c = 20-inc ;
        }
        
        if(trigger.isUpdate) {            
            if(inc != trigger.oldMap.get(con.Id).inc__c  && inc != null)
                con.dec__c = 20-inc;
            
            if(dec != trigger.oldMap.get(con.Id).dec__c && dec != null)
                con.inc__c = 20-dec;
        }
    } 
}
sai tarunsai tarun
Hi, try this one...............

trigger IncAndDec on Contact (before insert, before update) {
   
    for(Contact con : trigger.new) {          
        Integer dec =  con.dec__c;
        Integer inc =  con.inc__c;
        
        if(trigger.isBefore && trigger.isInsert) {  
            if(dec__c != Null)
                con.inc__c = 20-dec__c ;
            
            if(inc__c!= Null)
                con.dec__c = 20-inc__c ;
        }
        
        if(trigger.isBefore && trigger.isUpdate) {            
            if(inc__c != trigger.oldMap.get(con.Id).inc__c  && inc__c != null)
                con.dec__c = 20-inc__c;
            
            if(dec__c != trigger.oldMap.get(con.Id).dec__c && dec__c != null)
                con.inc__c = 20-dec__c;
        }
    } 
}