0

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.

enter image description here

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.

2
  • i think you're going to need to post the trigger(s) on Case - you may be inadvertently clearing the field target_check__c (remove from the post irrelevant stuff so we don't have to wade through a code dump)
    – cropredy
    Commented Jun 5, 2015 at 0:47
  • Yeah, I thought so too. So I created a new field on Case object and used that field for update. It feels like some issue related to asynchronous future call. I posted the code to my trigger.
    – sam_s
    Commented Jun 5, 2015 at 4:42

3 Answers 3

0

A small clarification, future method does not have SLA. That means you can't predict the time when it's gonna be executed. Most of the time it's a question of a few seconds. But it can actually take hours(I experienced personally until 4 hrs) if your org is having a big traffic. So if your future method takes a bit longer and in the meantime your records changes, it will change the behaviour of the process in your future method. Not sure it answer to your question but I hope it can help a bit.

2
  • right, but you can see in the image that target check first gets updated and then deleted for the first instance at 6:32 PM and similarly for all. So future method already executed and then case gets updated. I checked it from the debug logs also.
    – sam_s
    Commented Jun 5, 2015 at 0:30
  • but at 6.32 in the second transaction, the description changes as well somehow which might execute your trigger again and do something else, maybe call back your future method. Sorry not too sure what you do in your code exactly.
    – brovasi
    Commented Jun 5, 2015 at 0:39
0

You mentioned "However, If I modify any other field on the same case record without refreshing the page the Target Check value is getting deleted.". I would presume that you are retrieving the case records at one time only and then updating them constantly. If so, you'll still have old version of records while the records are being modified in backend by future methods.

So, when you are modifying any case record, try to retrieve those case records again, put in the required changes and update them. This should solve your issue. It would be helpful if you can share your code to to better understand the root cause.

3
  • I posted the code. For start of the future method, I'm querying the existing case records and I'm updating them at the end of it. I don't see any flaw in it.
    – sam_s
    Commented Jun 5, 2015 at 4:52
  • @sam_s, the issue is not in here as it seems to be fine. The problem probably is that once you have modified case, trigger is invoked, which schedules future method. I suspect that, your page refreshes with old value for "Target_check__c" field. As soon as you update the case record (without refreshing), it seems to overwrite the value of "Target_check__c". Are you using a visualforce page to edit case record?
    – Anshul
    Commented Jun 5, 2015 at 5:44
  • No, standard case object page layout. I checked the debug logs, my future method is calling only once at the time of after insert trigger. So if I update the record without refreshing and not changing the priority field...future method is not called but that update is somehow bringing the initial null value of Target Check field
    – sam_s
    Commented Jun 5, 2015 at 13:37
0

I think your problem is the following (in the future handler)

  1. You add to list casesToUpdate.add(cs) to set the target_check__c
  2. But, you update in the DML statement a different list - updateList where, of course, target_check__c is null

Tips

1 - better variable names would have avoided this

2 - The check for isEmpty is unnecessary; DML on empty lists don't consume any governor limit

1
  • Thanks for the tips. From the debug logs and query editor, I'm pretty sure to say that value gets updated from the future handler. I updated my question with a probable reason.
    – sam_s
    Commented Jun 6, 2015 at 17:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .