[SalesForce] APEX FATAL_ERROR System.CalloutException: Read timed out

I'm trying to make future callout from an Apex trigger to POST to an external webservice URL but getting a Read Time Out error.

Here are the issues:

  1. Why is the JSON not forming from the serialize method?
  2. Why does the setout time method not work?
  3. Why am I able to call from external applications like PostMan but not from Salesforce?
  4. Is there any workaround for this? Because if successful for next time getting a duplicate data error.

HTTP Class:

public static void IntegrationOppupdate(id opptyid) {
//Construct HTTP request and response
    //Http request method,Endpoint and setBody
    HttpRequest req = new HttpRequest();
    req.setHeader('Content-Type','application/json');
    String endpoint = 'https:TEST.COM';
    req.setMethod('POST');
    req.setEndpoint(endpoint); 
    req.setTimeout(2 * 60 * 1000);
    system.debug('Opportunity ID' +opptyid);
    //SOQL to construct JSON string in set body
    Opportunity Op=[select id,Name,Stagename,Integration_Type__c,Account.Name,Admin_Contact__r.Name,Admin_Contact__r.Phone,Admin_Contact__r.Email from opportunity where stagename='Closed-Won' and id=:opptyid];
    system.debug('without pretty'+JSON.serialize(Op));
    String JsonString=JSON.serialize(Op);
    req.setBody(JsonString);
    system.debug(JsonString);
    //Http response method 
    Http http = new Http();
    HTTPResponse res = http.send(req);
    System.debug(res.getBody());

Best Answer

We can't increase the timeout more than 120 sec since that is the maximum limit. The only workaround is to modify the query on the external webservice application to respond within 120 secs. Its suggestible to test the HTTP url from any on the REST applications like POSTMAN before making a decision in salesforce.

Related Topic