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
JohnDuraiJohnDurai 

Trigger to update Date Field

Hi - i want to create handler method for trigger to update the date field for the below scenario.
A BEFORE Trigger (INSERT / UPDATE) on Custom object to update Status_Changed_Date__c and Investigation_Completed_Date__c fields when the Resolution_Status__c field is updated.
For example, when Resolution_Status__c moved from "New" to "Resolved","In Progress" then I want to update Status_Changed_Date__c to today. 
Best Answer chosen by JohnDurai
AnkaiahAnkaiah (Salesforce Developers) 
try with below code.

apex trigger:
trigger dateUpdate on CustomObject__c (before update) {
    
    if(trigger.Isbefore && trigger.Isupdate){      
DateupdateHandler.updateResolutionDate(trigger.new,trigger.oldmap);
        
    }
}
Apex Class:
public class DateupdateHandler {
    
  public static void updateResolutionDate(List<CustomObject__c> newlist,Map<id,CustomObject__c> oldcases){
               
     for(CustomObject__c cs:newlist){
        CustomObject__c oldcase = oldcases.get(cs.id);
        If(cs.Resolution_Status__c != oldcase.Resolution_Status__c && cs.Resolution_Status__c=='In Progress'){           
            cs.Status_Changed_Date__c = system.today();                     
        }else If(cs.Resolution_Status__c != oldcase.Resolution_Status__c && cs.Resolution_Status__c=='Resolved'){  
            cs.Investigation_Completed_Date__c = system.today();
        }
        
    }
               
  }

}

If this helps, Please mark it as best answer.

Thanks!!
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi John,

As per my understanding,
when Resolution_Status__c field status  is moved to Inprogress, you need capture the date in Status_Changed_Date__c.
when Resolution_Status__c field status  is moved to Resolved, you need capture the date in Investigation_Completed_Date__c.

Am i correct??

Thanks!!
 
JohnDuraiJohnDurai
Hi @Ankaiah - Yes, When the resolution_status__c is moved from "New" or "Reopened" to "Resolved", "In Progress" or "Closed" , update Investigation_Completed_Date__c to TODAY
AnkaiahAnkaiah (Salesforce Developers) 
Hi John,

If my understaning is correct then try with below code.

change the object API name as per your org.
 
trigger dateUpdate on CustomObject__c (before update) {
    
  if(trigger.Isbefore && trigger.Isupdate){
            
    for(CustomObject__c cs:trigger.new){
        CustomObject__c oldcase = trigger.oldmap.get(cs.id);
        If(cs.Resolution_Status__c != oldcase.Resolution_Status__c && cs.Resolution_Status__c=='In Progress'){
            
            cs.Status_Changed_Date__c = system.today();
            
            
        }else If(cs.Resolution_Status__c != oldcase.Resolution_Status__c && cs.Resolution_Status__c=='Resolved'){
            
            cs.Investigation_Completed_Date__c = system.today();                       
        }
        
    }
  }
}
If this helps, please mark it as best answer.

thanks!!
 
JohnDuraiJohnDurai
Hi @Ankaiah - Thanks for response, can you suggest how to implement this in handler class seperately
AnkaiahAnkaiah (Salesforce Developers) 
try with below code.

apex trigger:
trigger dateUpdate on CustomObject__c (before update) {
    
    if(trigger.Isbefore && trigger.Isupdate){      
DateupdateHandler.updateResolutionDate(trigger.new,trigger.oldmap);
        
    }
}
Apex Class:
public class DateupdateHandler {
    
  public static void updateResolutionDate(List<CustomObject__c> newlist,Map<id,CustomObject__c> oldcases){
               
     for(CustomObject__c cs:newlist){
        CustomObject__c oldcase = oldcases.get(cs.id);
        If(cs.Resolution_Status__c != oldcase.Resolution_Status__c && cs.Resolution_Status__c=='In Progress'){           
            cs.Status_Changed_Date__c = system.today();                     
        }else If(cs.Resolution_Status__c != oldcase.Resolution_Status__c && cs.Resolution_Status__c=='Resolved'){  
            cs.Investigation_Completed_Date__c = system.today();
        }
        
    }
               
  }

}

If this helps, Please mark it as best answer.

Thanks!!
 
This was selected as the best answer