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
davidleckenbydavidleckenby 

I need to pass back the response result (String) from a Web Service call class to a trigger class

Hi -   I am wanting to pass back the response string from this Webservice class, which is void by nature, to the trigger that is calling it. I have tried various methods by using a primitive string reference and also creating a new class with s string variable inside it but havent been able to get the string result back. Below is the code for the Webservice class.
public with sharing class ELO_Webservice {
    @future (callout=true)
    public static void CallWebService(String body, String respStr)
    {
       Http http = new Http();
		HttpRequest request = new HttpRequest();
		request.setEndpoint();
		request.setMethod('POST');
		request.setHeader('Content-Type', 'application/json;charset=UTF-8');
		// Set the body as a JSON object
		//System.debug(body);
        request.setBody(body);
       	');
        
        HttpResponse response = http.send(request);

		// Parse the JSON response
		if (response.getStatusCode() != 200) {
    		System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus());
		} else {
    		System.debug(response.getBody());
		} 
       
        respStr.responsestr = response.getBody();
        System.debug('responsestr = ' + respStr.responsestr);
        
    }


This is the call from the trigger
ELO_Webservice.CallWebService(body, eResponse.responsestr);
Hope this is clear. Many thanks

 
Best Answer chosen by davidleckenby
Raj VakatiRaj Vakati

Callouts cannot be made from triggers as that would hold up the database transaction until the callout completed, which can be up to 120 seconds from a limits perspective. the callout from a trigger is to schedule it to run asynchronously,so you can't wait for a response from the callout as that would require the transaction to stall until it completes, which is the reason that synchronous callouts aren't allowed.


There are the options 
  1. Call the web service on the button click and handler the logic 
  2. or handler the  logic in the after getting the response 
  3. if you have any logic dependence on the web services, move the logic into the callouts and handler the logic after getting the response 

All Answers

Raj VakatiRaj Vakati

Callouts cannot be made from triggers as that would hold up the database transaction until the callout completed, which can be up to 120 seconds from a limits perspective. the callout from a trigger is to schedule it to run asynchronously,so you can't wait for a response from the callout as that would require the transaction to stall until it completes, which is the reason that synchronous callouts aren't allowed.


There are the options 
  1. Call the web service on the button click and handler the logic 
  2. or handler the  logic in the after getting the response 
  3. if you have any logic dependence on the web services, move the logic into the callouts and handler the logic after getting the response 
This was selected as the best answer
davidleckenbydavidleckenby
Thanks Raj - think Ill try option 3