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 updated data of that field on a record detail page.
However, If I modify any other field on the same case record without refreshing the page the Target Check
value is getting deleted. This is a new field and does not have any workflow rules on it. Could anyone help me out please?
Below it the case history of fields.
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;
}
}
Update-II
Since I'm beginner in SFDC, I'm not 100% confident about this. The data in the field is updated in database but the view state of visualforce page does not have that updated value. When we are trying to inline-edit some fields on the page and save the page, the field values on the page over-rides the values in the data-base. That is the reason why Target Check
in this scenario is getting deleted.
Work-around Create a formula field which shows the value of that Target Check
and hide the Target Check
from the page-layout.
Case
- you may be inadvertently clearing the fieldtarget_check__c
(remove from the post irrelevant stuff so we don't have to wade through a code dump)