You need to sign in to do that
Don't have an account?
Mario 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.
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++; } } }
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:
Let me know if you still face any issue.
Thanks,
Abhishek Bansal.
All Answers
You are heading in the right dierection. Just a couple of changes in your trigger:
Please let me know if you need any further help on this.
Thanks,
Abhishek Bansal.
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?
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:
Let me know if you still face any issue.
Thanks,
Abhishek Bansal.