[SalesForce] How to set timeout for web service call

I want to call out my web service after the insert process of a custom object with a delay of 2 minutes. But i am getting that exception FATAL_ERROR|System.CalloutException: Read timed out How can i fix that problem ? Here is my implementation.

Callout class:

public class HttpCallout{
    @Future(callout=true)
    public static void getContent(String url){
        Http h = new Http();

        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
        req.setTimeout(2000);

        HttpResponse res = h.send(req);
        String response = EncodingUtil.urlDecode(res.getBody(),'UTF-8');
        System.debug('***response****'+response);
    }

    public void serviceCaller(){
        getContent(url);
    }
}

Trigger:

trigger AfterInsertOrUpdate on Sales_Order__c (after insert) {
    System.debug('Making future call to update account');
    HttpCallout httpCallout = new HttpCallout();
    httpCallout.serviceCaller();
    System.debug('After Insert');
}

Any help would be appriciated.

Best Answer

The argument to HttpRequest.setTimout is a millisecond value so for 2 minutes:

req.setTimeout(2 * 60 * 1000);

PS With the clarification that the desire is to make the web service call after a 2 minute delay I suggest you experiment with this Need System.schedule cron string for run once approach. In your case you would build the cron string to be 2 minutes from the current date/time.

Related Topic