function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TempleHoffTempleHoff 

Question about trigger @future and screen refresh

I have a trigger that works:

trigger setGeo on CarQuotes__c (before insert, before update) {

for(CarQuotes__c Car : trigger.new)
    if (Car.NeedDistance__c == True){
        ZipGeoCode.getDistance(Car.Id);
    }
    else { Car.NeedDistance__c = True; }
}

 

My class ZipGeoCode works as well.  It uses the @future and calls the Google api to get a driving distance and populates a field on the CarQuotes__c record.

 

The problem is that when you create a new record or update a record, when you click save what you see does not reflect the distance that was gathered from Google.  But if you refresh or navigate away from the record and back to it, you see the distance value was in fact updated.  This is deceptive for the users.  Is there any way to have the trigger pause until the ZipGeoCode is finished?  Or on edit/update to force the browser to refresh?  So that the users view of the record after save shows the value?

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
ZipGeoCode runs "out of band", so it will never complete before the trigger does-- in fact, it is guaranteed to run after the trigger completes. The request goes into a queue, so it may not complete for seconds, minutes, or, in rare cases, hours after it was requested. The best you can do is have a custom field on the record that includes some sort of note that states that the location is "updating" and will be available later.

All Answers

sfdcfoxsfdcfox
ZipGeoCode runs "out of band", so it will never complete before the trigger does-- in fact, it is guaranteed to run after the trigger completes. The request goes into a queue, so it may not complete for seconds, minutes, or, in rare cases, hours after it was requested. The best you can do is have a custom field on the record that includes some sort of note that states that the location is "updating" and will be available later.
This was selected as the best answer
TempleHoffTempleHoff

Kinds of what I thought.  Thanks.