+ Start a Discussion
Russell baker 1Russell baker 1 

Trigger to get Prior value of formula field on opportunity

I have custom formula field return type date name "Last Activity date" on opportunity It is showing me Last Activity date. I want to capture "last to last Activity date". So I create a custom date field name " last to last activity date" and tried to update via workflow with "created and every time edited" and formula evaluated true. formula is <IsChanged (Last_Activity_due_date__c)>
workflow action is field update "Last to Last Activity Date"(Last_to_Last_Activity_Date) date type Date and formula is
<PRIORVALUE(Last_Activity_due_date__c)>

But It is not updating the Last to Last Activity Date field. I am thinking formula field change does not work with workflow. So i think befor trigger can solve my purpose.
Please help me  to write a trigger.

 
Arpit Jain7Arpit Jain7
Hey Russel,

You can try below trigger for your requirement

trigger trig_update_old_last_activity_date(before update)
{
    for(opportunity opp:trigegr.new)
    {
        opp.Last_to_Last_Activity_Date = trigger.oldmap.get(opp.id).your_lastactivitydate_field_apiname;
    }    
}

Hope it helps !
 
Russell baker 1Russell baker 1
Hi Arpit,
No, Its not working. i have poseted below code:
 
trigger trigafupdate on opportunity (before update){
    
      for(opportunity opp:trigger.new)
      {
        opp.Last_to_Last_Activity_Date__c = trigger.oldmap.get(opp.id).Last_Activity_Due_date__c;
    }    
}

It is not updating Last to last activity date field.
Russell baker 1Russell baker 1
trigger trigafupdate on opportunity (before update) {
     List<opportunity> toUpdate = new List<opportunity>();
    for (opportunity opps : trigger.new)
    {
        Date newVal=opps.Last_to_Last_Activity_Date__c;
        Date oldVal=trigger.oldMap.get(opps.id).Last_Activity_Due_date__c;

        if (newVal!=oldVal)
        {
           opps.Last_to_Last_Activity_Date__c=oldVal;
           toUpdate.add(opps);
        }
    }
    }
I tried above trigger but not upadating field.
 
Arpit Jain7Arpit Jain7
Hey Russel,

Issue is coming because formula field change does not call any workflow or trigger and this might be the reason field is not getting updated. So may be you need to use some work-flow for updating last activity date field in sfdc and which will call your workflow or trigger and then field will get updated correctly in sfdc.

Thanks.

 
Amit GhadageAmit Ghadage
Hi 
Russell baker 1,
Set History tracking for Last_Activity_Due_date__c
in trigger 
trigger trigafupdate on opportunity (before update){
    Map<Id,opportunityHistory> opportunityHistoryMap = new Map<Id,opportunityHistory>();
for(opportunityHistory oppH:SELECT OpportunityId, Last_Activity_Due_date__c FROM OpportunityHistory where OpportunityId = IN :                  Trigger.New )
      {integer i = 0;
         if(i == 1)
        {
            opportunityHistoryMap.put(oppH.OpportunityId, oppH);
            i = 0;
       }
      i++;
}

      for(opportunity o:Trigger.New )
      {
        opp.Last_to_Last_Activity_Date__c =           opportunityHistoryMap.get(opportunityHistoryMap).Last_Activity_Due_date__c;
    }    
}

Best Regards,
Amit Ghadage.
Russell baker 1Russell baker 1
Hi, I can not set histpry tracking of formula field. Last_Activity_Due_date__c is formula field retur type date.
Amit GhadageAmit Ghadage
Hi Russell baker 1,
Insread of Last_Activity_Due_date__c set history tracking for fields which  you are using in formula of Last_Activity_Due_date__c.
and  replace Last_Activity_Due_date__c with those fileds in above trigger .
apply same formula in trigger on  history (i.e prior to prior values of ) those fields and assign it to Last_Activity_Due_date__c.

Best Regards,
Amit Ghadage.