I have a field called Target Check
on Case object which was updated by future call from a AfterInsert
and AfterUpdate
trigger. Since it is a future method, there is a 2-3 second delay and I have refresh the page to view that valueupdated data of that field on a record detail page.
Update
Can't figure it out. Need some reasoning on execution of future methods. From my understanding, we definitely need to refresh the page in-order to render the data from future method trigger. Is it mandatory?
Trigger:
if(Trigger.isAfter)
{
if(Trigger.isInsert)
{
set<Id> caseIds = new set<Id>();
for(Case cs:Trigger.New)
{
if(cs.Priority!=null)
{
caseIds.add(cs.Id);
}
}
if(caseIds.size()>0) //also using a static method to prevent recursion
{
UpdateTargetCheckClass.updateField(caseIds);
}
}
if(Trigger.isAfter)
{
set<Id> caseIds = new set<Id>();
for(Case cs:Trigger.old)
{
Case newCase = Trigger.newMap.get(cs.Id);
if(cs.Priority!=newCase.Priority){
caseIds.add(cs);
}
}
//call above future method here and static method to prevent recursion
}
}
Handler:
@future
public static void updateField(set<Id> newCaseIds)
{
list<Case> casesToUpdate = new list<Case>();
// I have to pull the data from related CaseMilestone which made me to use future annotation
list<Case> updateList = [Select Id,
Target_Check__c,
(SELECT Id, TargetDate
FROM CaseMilestones limit 1)
from Case where Id IN: newCaseIds];
for(Case cs: updateList)
{
CaseMilestone cm = cs.CaseMilestones.size() > 0 ? cs.CaseMilestones[0] : new CaseMilestone();
if(cm.TargetDate!= null && cs.Target_Check__c != cm.TargetDate)
{
cs.Target_Check__c = cm.TargetDate;
system.debug('---------Target_Check__c---------'+cs.Target_Check__c);
casesToUpdate.add(cs);
}
}
if(!casesToUpdate.isEmpty())
{
update updateList;
}
}