3

Http request using restAPI :

public static String sendFormHttp(Opportunity opportunity, List<Easy_Send_Form_Mapping__mdt> mappingList, Map<String, String> systemVariableMap){
        String bodyReq = setFormJson(opportunity,mappingList,systemVariableMap);
        System.debug('##############  '+bodyReq);
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(systemVariableMap.get('ES_Endpoint'));
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setTimeout(10000);
        // Set the body as a JSON object
        request.setBody(bodyReq);
        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());
                return 'ERROR';
        } else {
            System.debug(response.getBody());
            Map<String,Object>responseMap = (Map<String,Object>)JSON.deserializeUntyped(response.getBody());
                System.debug(responseMap);
                System.debug(responseMap.get('link'));
                return (String)responseMap.get('link');
        }

    } 

This http request work well , but when I try it for the first time of the day or if i didn't send it few hours before I am getting Read Time Out Error.

any ideas are welcome ?

  • 2
    It does not look like the SF issue. It can happen the 3rd party is hosted on some cloud tier which sleeps overnight due to inactivity and when you try next morning it wakes up slowly giving you timeout – Pranay Jaiswal Jun 5 at 11:59
  • 2
    You can increase timeout 120*1000 – Pranay Jaiswal Jun 5 at 12:06
  • Thank you Pranay Jaiswal, yes I think the problem come from the 3rd party . But how can I tell them to fix it ? I didn't want to increase the timeout because I am supposed to display the response . – Raphael D Jun 5 at 12:20
  • i dont know the architecture of 3rd party so don't know how they implemted. Can be a major task for them. Ask them if they can for service to never sleep.. But that would mean that instance will be awake all night giving you thousands of pounds in bills. – Pranay Jaiswal Jun 5 at 12:23
  • 2
    Or another approach would be to wrote a scheduler thsr runs an hour before the business nd does a dummy callout to that endpoint waking them up – Pranay Jaiswal Jun 5 at 12:26
3

I've run into this in the past, and there have been a few other stack exchange posts on this. The problem is with the external web service, and the most practical approach is one of the 2 options:

  1. Run a scheduled batch job that runs hourly (or as you need) that calls out to the other system.
  2. If you get an exception, retry the call out via async apex.

Finally, document the issue and show the logs to the provider that you're working with. You might be able to get the fix prioritized.

  • thank you , I cannot use async apex because I need to display the callback response .But I don't get exception anyway just taking something like 25 sec the first time and 1 or 2 sec after. – Raphael D Jun 5 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.