You need to sign in to do that
Don't have an account?
Joseph A
Making HttpRequest "before update" - Callout from triggers not supported
Hi,
I need to run some verifications in trigger "before update" against a remote API and perform future actions based on its response . I found that Apex class function cannot return any response due to synchronous / asynchronous issues between triggers and HttpResponse. Is there any way to go around?
Refer https://help.salesforce.com/articleView?id=000133752&language=en_US&type=1
All Answers
1. How the trigger gets called? Is it from VFpage/Class or standard page?
2. Can the first call be asynchronous?
Option 1: If triggers gets called from apex class, then before saving record make a callout and store the response and save record. Now, in trigger you would know the response of the first call so based on that you can make asynchronous callout using future methods.
Option 2: Use Future method to make callout and read the response and make another callout, all calls will be asynchronous so you have to decide if this fits for your scenario.
When a sales rep trying to update customer email, the system should verify if email doesn't belong to another customer and this verification must be running agains a remote database where email address is primary key.
Here is what I have so far:
You cannot get callout response in trigger in realtime, since it is async response will be asycnhronous.That means in the future method you have to write a logic to read the response and based on that you can update your account email address field. If it errors out you can send an email or create a new field on account to store the response if you want.
Here is the sample.
trigger EmailChange on Account (before update) {
for(Account a : trigger.new){
if( Trigger.oldMap.get( a.Id ).Email__c != Trigger.newMap.get( a.Id ).Email__c ){
//You cannot get response here, it is async so you cannot read response
string APIResponse = APIConnect.makePostCallout('email',Trigger.newMap.get( a.Id ).Email__c,a.Id);
//a.addError(APIResponse);// add more functionality here
System.debug(APIResponse);
}
}
}
public class APIConnect {
@future(callout=true)
public static string makePostCallout(string mode, string var,String accountID) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://myserver.com/sftest/');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
request.setBody('{"key":"3434545","mode":"'+mode+'","var":"'+var+'"}');
HttpResponse response = http.send(request);
//Read response.
if(sucesfulresponse)
{
//Update email address on account
Account accRecord = new Account(id= accountid, Email__c = var);
update accRecord;
}
else
{
//Error
//you can send an email
//Or ceatea new field to store the response.
Account accRecord = new Account(id= accountid, newfield = response.getBody());
update accRecord;
}
}
}
Another question: is there any way to display a system dialog / alert if an update fails?
Is it because I'm trying to update the same affected row?
Thanks!
Refer https://help.salesforce.com/articleView?id=000133752&language=en_US&type=1
Why do you have to update it to old email address if it fails? You dont have to do update email address again with the same value as it has not changed.
Recursive flags would not work in this scenario, trigger is getting fired from ASYNC method and that resets all static flags( as new transaction).
you can skip calliing future method by checking system.isFuture(), this tells you if code is executed by future method.
if(!System.isFuture())
APIConnect.makePostCallout('email',Trigger.newMap.get( a.Id ).Email__c,a.Id);