I have a void callout class. But I need take all these tools through Batch to make it in Schedule later. How can I convert it into Batch structure with start, execute, finish?

public static void getTool(){
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    HttpResponse response = http.send(request);  

    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    List<Object> receivedTools = (List<Object>) results.get('cards');
    List<Tool__c> newTools = new List<Tool__c>();

    for(Object eachTool : receivedTools){
        String inText = String.valueOf(eachTool);
        String name = inText.substringBetween('name=', ',');
        String description = inText.substringBetween('desc=', ',');

        Tool__c newTool = new Tool__c(
            Name = name,
            Description__c = description,
    insert newTools;
  • If your intent is to execute this at a later point, this isn't a batch but rather is a Schedulable that you schedule using System.schedule. You can do these as one-off executions or as recurring executions based on the cron pattern you use. See developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – Phil W Jul 5 '19 at 14:17
  • @PhilW we can't do callouts from the scheduler, we have to use schedularbatch – Pranay Jaiswal Jul 5 '19 at 14:19
  • Schedule it then delegate to the @future method that does the callout. See developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… (it even provides skeletal example of using this for a callout) – Phil W Jul 5 '19 at 14:21
  • 1
    @MichaelLev19, you can only iterate objects with a batch - the start method must return an SOQL query locator or iterator for the objects that are to be passed to start. It seems to me you don't have any objects to iterate, so you can't use a batch, unless you have a "fake" object that you run the batch against every time (this will call execute once for that "fake" object, assuming there's only the one). I don't think this is the right approach. – Phil W Jul 5 '19 at 15:19
  • 1
    @PranayJaiswal, ETL may be the right way to go, if you have some external system that can control that execution. If, on the other hand, you have no other external system that can do that (or you must do a pull from the external system rather than a push from it) then use of a Schedulable is fine. Running every 5 minutes uses just 576 async executions in a 24hr period (assuming one for the Schedulable and one for the future) which really isn't significant. – Phil W Jul 5 '19 at 15:22

If you need to write something that must pull data from the remote server (i.e. you can't use some external ETL tool) I think you can do this with a schedulable and a future method relatively easily:

public with sharing class PullSchedulable implements Schedulable {
    public void execute(SchedulableContext context) {

    @future(callout = true)
    public static void doCallout() {
        // Your code here

You then have to arrange to schedule this code via System.schedule in a manner appropriate to your needs (as mentioned, schedule 12 instances of the class at 5 minute intervals and recurring every hour forever gives you a resilient execution every 5 minutes).

This will consume some of your async executions limits, but very few. Every execution will use 2 calls, and doing this every 5 minutes 24 hours a day will use 576 async executions per day (so not going to cause you to break any limits).

| improve this answer | |
  • Hello. And can we use it for lightning component? I'm trying to make page refreshing without page reloading, with callout and schedule. – Монте Кристо Jul 7 '19 at 9:07
  • You would need to consider use of Streaming API so the client can detect changes to some data you update when the callout finishes. – Phil W Jul 7 '19 at 9:34

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.