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
Mario Conteddu 11Mario Conteddu 11 

Tracking number of date field changes and difference between new and old date

Here is what I need to achieve:
track how many times BDM_Projected_Launch_Date__c gets changed in Count_of_PLD_changes__c
calculate difference in months between the very first BDM_Projected_Launch_Date__c and last one in of_Months_since_1st_PLD__c
the very first BDM_Projected_Launch_Date__c value is recorded on PLD_first_Value__c and it gets populated at record creation by a WFR

here is what I've done so far but it's not really working.
trigger ProjectedLiveDateTracking on Opportunity (before update) {

  Map<Id,Opportunity> o = new Map<Id,Opportunity>();
  
    o = trigger.oldMap;
    
    for(Opportunity newopp: trigger.new)
    {
        
        
        if(newopp.BDM_Projected_Launch_Date__c != o.get(newopp.Id).BDM_Projected_Launch_Date__c)
        
        {
        
        Date old = o.get(newopp.Id).PLD_first_Value__c;
        Date first = newopp.BDM_Projected_Launch_Date__c;
        Integer monthDiff = old.monthsBetween(first);
        if (first.day() > old.day()) monthDiff++;
        System.debug(monthDiff);
        newopp.of_Months_since_1st_PLD__c = monthDiff;
        newopp.Count_of_PLD_changes__c++;

        }
        
    }
}



 
Best Answer chosen by Mario Conteddu 11
Abhishek BansalAbhishek Bansal
Hi Mario,

This is because your field Count_of_PLD_changes__c is null during the first update of your BDM_Projected_Launch_Date__c. Please update the code as below:
trigger ProjectedLiveDateTracking on Opportunity (before update) {

  Map<Id,Opportunity> o = new Map<Id,Opportunity>();
  
    o = trigger.oldMap;
    
    for(Opportunity newopp: trigger.new)
    {
        if(newopp.BDM_Projected_Launch_Date__c != o.get(newopp.Id).BDM_Projected_Launch_Date__c){
			newopp.of_Months_since_1st_PLD__c = (newopp.PLD_first_Value__c).monthsBetween(newopp.BDM_Projected_Launch_Date__c);
			if(newopp.Count_of_PLD_changes__c == null){
				newopp.Count_of_PLD_changes__c = 1;
			}
			else{
				newopp.Count_of_PLD_changes__c++;
			}
        }
        
    }
}


Let me know if you still face any issue.

Thanks,​
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi Mario,

You are heading in the right dierection. Just a couple of changes in your trigger:
trigger ProjectedLiveDateTracking on Opportunity (before update) {

  Map<Id,Opportunity> o = new Map<Id,Opportunity>();
  
    o = trigger.oldMap;
    
    for(Opportunity newopp: trigger.new)
    {
        if(newopp.BDM_Projected_Launch_Date__c != o.get(newopp.Id).BDM_Projected_Launch_Date__c){
			newopp.of_Months_since_1st_PLD__c = (newopp.PLD_first_Value__c).monthsBetween(newopp.BDM_Projected_Launch_Date__c);
			newopp.Count_of_PLD_changes__c++;
        }
        
    }
}

Please let me know if you need any further help on this.

Thanks,
Abhishek Bansal.
Mario Conteddu 11Mario Conteddu 11

Thanks Abishek.
I am still getting the same type error: 

Error:Apex trigger ProjectedLiveDateTracking caused an unexpected exception, contact your administrator: ProjectedLiveDateTracking: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.ProjectedLiveDateTracking: line 11, column 1

Any idea?

Abhishek BansalAbhishek Bansal
Hi Mario,

This is because your field Count_of_PLD_changes__c is null during the first update of your BDM_Projected_Launch_Date__c. Please update the code as below:
trigger ProjectedLiveDateTracking on Opportunity (before update) {

  Map<Id,Opportunity> o = new Map<Id,Opportunity>();
  
    o = trigger.oldMap;
    
    for(Opportunity newopp: trigger.new)
    {
        if(newopp.BDM_Projected_Launch_Date__c != o.get(newopp.Id).BDM_Projected_Launch_Date__c){
			newopp.of_Months_since_1st_PLD__c = (newopp.PLD_first_Value__c).monthsBetween(newopp.BDM_Projected_Launch_Date__c);
			if(newopp.Count_of_PLD_changes__c == null){
				newopp.Count_of_PLD_changes__c = 1;
			}
			else{
				newopp.Count_of_PLD_changes__c++;
			}
        }
        
    }
}


Let me know if you still face any issue.

Thanks,​
Abhishek Bansal.

This was selected as the best answer
Mario Conteddu 11Mario Conteddu 11
Thanks makes sense Abhishek.Thanks for your help! It works!