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
WellSky Integration UserWellSky Integration User 

Failed to invoke future method error

I am constantly getting an email with the error below.  I have tried changing the setTimeout value and that does not seem to work.  I am not sure if it is the code that is causing it or maybe the endpoint being to slow.

The idea is to enter an Account record that is sent to the endpoint.  The endpoint creates a record, then sends back To SalesForce an update with the associated id number from the record that was created on the endpoint's side.  It is looking as if most (not all) transactions cause the error/warning.  There have even been times where a duplicate record was attempted to be added on the endpoints side - which I am thinking is due to the time of the response being so long the transaction is sent again....but that is just a guess at this point.

Apex script unhandled exception by user/organization: 0055Y00000HMWkA/00D5Y000002W93U

Failed to invoke future method 'public static void putDestination(String)' on class 'AccountDestinationCallout' for job id '7075Y0000BUIrcf'

caused by: System.CalloutException: Read timed out

Class.AccountDestinationCallout.putDestination: line 45, column 1
Line 45 is :
HTTPResponse objRes = objHttp.send(objReq);


The code for AccountDestinationCallout:
public class AccountDestinationCallout {
    public static String generatedToken {set; get;}
    static String errorMessage {set; get;}
    static String TimelineResponse {set; get;}
    
    @future(callout = true)
    
// called by trigger AccountUpdate
    
    public static void putDestination(string buildMessage) {    
		HttpRequest req = new HttpRequest();
		req.setMethod('POST');
	    req.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf('Sales' + ':' + 'someid')));
    	req.setHeader('content-type', 'application/x-www-form-urlencoded');
        req.setEndpoint('https://homedomainname.com/api/prod_gateway/path');
		req.setBody('grant_type=client_credentials');

	    Http http = new Http();
		req.setTimeout(120000);
   	   	HTTPResponse res = http.send(req);
     	JSONParser objParse = JSON.createParser(res.getBody());
        while (objParse.nextToken() != null) 
        {
           if (objParse.getCurrentToken() == JSONToken.FIELD_NAME && objParse.getText() == 'access_token')
           {                               
              objParse.nextToken();
              generatedToken = objParse.getText();
           }
        }
        	if(generatedToken == null ) 
            {
            	TimelineResponse = 'Error while generating token, so unable to get messages. Check your debug log.';
            	return;
        	}

	        HttpRequest objReq = new HttpRequest();
 
        objReq.setEndpoint('https://homedomainname.com/api/prod_gateway/SalesForce/postSF');
        	objReq.setMethod('POST');
            objReq.setHeader('Authorization', 'Bearer ' + generatedToken);
            objReq.setBody(buildMessage);
	        Http objHttp = new Http();
            objReq.setTimeout(20000);
    	    HTTPResponse objRes = objHttp.send(objReq);
        	TimelineResponse = objRes.getBody();
    	    if(String.isBlank(TimelineResponse))
            {
        	   TimelineResponse = objRes.toString();
            }
    }
}


 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

A System.CalloutException: Read Timed Out exception usually occurs when the target system is slow to respond to the callout request issued to/from Salesforce. 
System.CalloutException: Read timed out exceptions can also surface when firewalls are involved. If your organization has a firewall in place, check with them if there is a delay in your request being processed.

Can you try to increase the setTimeout to 20000 for other call out as well.

Please fine the below article for more information on the same.

https://www.levelupsalesforce.com/read-timed-out

Thanks,