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
RstrunkRstrunk 

Apex Callout

 

   Since apex callouts are asynchronous, can I have a trigger that will prevent a record from being saved if the return value of the callout meets a certain criteria?

 

An example being:

  When a record is being saved, the trigger calls out to a web service using the value in one of the fields on that record.  If the web service returns 0 I want the record to not be allowed to be saved.  If the record returns a 1 I want the record to be saved.  

 

Anyone have experience with matters such as this? 

Abhi_TripathiAbhi_Tripathi

Hey Rs,

 

You can write a trigger and do the call in loop like

 

//Do your callout

 

//Check for result here

if(Account tr : Trigger.new) {

 

//condition satisfies

insert tr;

 

else {

 

//Here adding error message when condition matches using label
tr.addError(label.AccountTriggerErrorMessage);

 

}

 

You can create an error message for the trigger on detail page using custom labels

 

Here is the link too

http://abhithetechknight.blogspot.in/2013/09/custom-error-message-on-standard-edit.html

 

 

 

RstrunkRstrunk

Abhi, 

  I appreciate your response.  I am a little confused though.  From what I have been researching, all callouts are annotated with @future which means that the callout method will run after the trigger has long finished.  Otherwise, the record save would be held up waiting for the response from the service.