We all have heard about APIs. These small web applications help us to fetch data and resources that are externally stored on some other site. In order to fetch the data from these links, we need to make a callout to the external resource. Normally in APEX, we make a callout from the HTTP and HTTPRequest class. However when it comes down to scheduling a job to send callout then things get pretty messy as you cannot send callouts from scheduled jobs directly. They need to be defined in the @future annotation. This is what I’ll demonstrate, callout from scheduled jobs.

APEX code for callout from scheduled jobs

Let’s jump to the code for this one:

public class getAnimalNameById implements schedulable{
    public void execute(SchedulableContext sc){
        getAnimalNameById(1);
    }
    
    @future (callout=true)
	public static void getAnimalNameById(integer i){
        string str;
        HTTP http = new HTTP();
        HTTPRequest req = new HTTPRequest();
        req.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+i);
        req.setMethod('GET');
        HTTPResponse res = http.send(req);
        Map results = (Map)JSON.deserializeUntyped(res.getBody());
        String results2 = String.valueOf(results.get('animal'));
        results2 = results2.substringAfter('name=');
        results2 = results2.substringBefore(',');
        system.debug(results2);
    }
}

In the above example, a schedulable class is created and the data is retrieved in it through a callout. You can schedule a job now and check its functioning in the developer console by debugging it. Also, you may manually go to the link and see the values there.

Support

That’s all about sending callout from scheduled jobs in Salesforce, for any further queries feel free to contact us at:

https://wedgecommerce.com/contact-us/

Or let us know your views about this blog in the comments section below.

Leave A Reply

Please verify that you are not a robot.

Tell us about Your Company

How can we help you with your business?




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home