2

I have a Salesforce trigger that gets fired whenever the firstName field in the Application object gets updated. Once it happens, I want to make an Http call and get the response to be used in another function in the trigger right after the callout. So, the callout should be synchronous, as I need the response to be used in another function.

(I know that for asynchronous functions I should use the @future annotation, but I want to make a synchronous callout.)

Does anyone have any idea?

Here is the trigger:

trigger TestAppTrigger on Application__c (after update) {

    private Boolean testVal = false;
    
    for (Application__c app: Trigger.new) {
     
     
        Application__c oldApp = Trigger.oldMap.get(app.Id);
        if (oldApp.firstName != app.firstName){ 
                                  
           testVal = TestAppTriggerClass.Test('10');

           //I need to take the response and pass it in to the following function:

           TestAppTriggerClass.Test2(testVal);           
    
        }       
        
    }      
}

And here is the class that holds the functions:

public class TestAppTriggerClass {

     
   public static Boolean Test(String studentId) {
         
        Boolean test;
                   
        //sending the http request and receiving the response
        List<Object> responseArray= HelperClass.httpFunction(studentId); 

        if (responseArray.isEmpty()) test = false;         
        else test = true;   
     
        return test;
     }


   public static void Test2(Boolean testVal) {
     //Do something
}

1 Answer 1

3

Salesforce does not allow us to make synchronous callouts from triggers.

You need to do this async, there is no way around it.

Though instead of having your trigger call Test1() and then Test2()...
there's nothing stopping you from having your trigger call Test1() (which is the method that makes the callout, the one that must be asynchronous/run in a different transaction)
and then Test1() internally calling Test2()

4
  • The reason why is that long callouts causes database contention, so we are prohibited from doing this in order to make access to resources more reliable.
    – sfdcfox
    Apr 29, 2023 at 5:15
  • Thank you for your response, I tried what you suggested. However, because Test2() is also an asynchronous function, when I call it internally, I get an error saying it is not possible to call a future method inside another! May 3, 2023 at 22:51
  • @RoseMirzaei Was Test2() always @future? The example you provided doesn't indicate that it is. Since we can't read minds or see your screen, it's important to provide details and be accurate here. It's very difficult to provide accurate, useful answers otherwise. You should edit your question to address that. For now, about all I can say is that Test2() should not be @future (it'll still be run in the same async context as Test1(), provided you call it from Test1()). If there is a reason to do it in two separate async contexts, then you'd want to use Queueable instead.
    – Derek F
    May 3, 2023 at 23:16
  • You are right, I should have mentioned that Test2() is also a future method. I used the Queueable classes and now the problem is solved! Thank you for your help, I appreciate it. May 4, 2023 at 22:17

You must log in to answer this question.

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