function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tom SimmonsTom Simmons 

Help with @future to batch class

Dear all,
Below is my callout code which is getting data from other Salesforce instance. Code work fine, however instead of @future, I want this code to run on daily basis (once a day) to get all the records from other Salesforce instance. Since I`m new to salesforce, can someone please help me in converting this code to batchable class? Also, is there any other best way to accomplish daily callouts? Please suggest... 
 
public class SampleIntegration
{
        @future (callout=true)
    public static void integration()
    {
        HttpRequest obj = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        String username = 'tom.simmons@hotmail.com';
        String pwd = 'Welcome123rwnYJye3ZaZlxCCq9UJ7sD0AE';
        String reqBody = 'grant_type=password&client_id=3MVG9Y6d_Btp4xp4D3Z8GAwN6wIbOw3vQabsximESJjFoCgpNad5tVtSnQIZN7xAyCekd0tyknn95VTImvYJ2&client_secret=817876896827445493&username='+username+'&password='+pwd;
        obj.setMethod('POST');
        obj.setEndPoint('https://login.salesforce.com/services/oauth2/token');
        obj.setBody(reqBody);
        res = http.send(obj);
                                system.debug('Oauth Response: '+res.getbody());
        OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);
                     system.debug(objAuthenticationInfo);   
                System.debug('objAuthenticationInfo'+objAuthenticationInfo);


        Http h1 = new Http();
        HttpRequest req1 = new HttpRequest();
        req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
        req1.setHeader('Content-Type','application/json');
        req1.setHeader('accept','application/json');
        req1.setMethod('GET');
        String strName = 'Hello 3';
        req1.setEndpoint('https://na1.salesforce.com/services/apexrest/Dev_tom/Account1');//URL will be your Salesforce REST API end point where you will do POST,PUT,DELETE orGET
        system.debug('======req1========'+req1);
        HttpResponse res1 = h1.send(req1);
        system.debug('==========res1============'+res1.getBody());

                        JSON2Apex Jsondes = (JSON2Apex)JSON.deserialize(res1.getbody(), JSON2Apex.class);
                                                                System.debug('Jsondes'+Jsondes);

                                                Account A = New Account ();
                                                A.Name = Jsondes.Name;
                                                A.Site = Jsondes.ID;
                                                insert A;


    }

                }


 
Best Answer chosen by Tom Simmons
karthikeyan perumalkarthikeyan perumal
Hello, 

i forget to add the query in start method,
use below Code, 
global class FetchOtherOrgDetails implements Database.Batchable<sObject>,Database.AllowsCallouts{

   global Database.QueryLocator start(Database.BatchableContext BC){
     String query = 'Select id  From Account Limi 1';
        return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
    HttpRequest obj = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        String username = 'tom.simmons@hotmail.com';
        String pwd = 'Welcome123rwnYJye3ZaZlxCCq9UJ7sD0AE';
        String reqBody = 'grant_type=password&client_id=3MVG9Y6d_Btp4xp4D3Z8GAwN6wIbOw3vQabsximESJjFoCgpNad5tVtSnQIZN7xAyCekd0tyknn95VTImvYJ2&client_secret=817876896827445493&username='+username+'&password='+pwd;
        obj.setMethod('POST');
        obj.setEndPoint('https://login.salesforce.com/services/oauth2/token');
        obj.setBody(reqBody);
        res = http.send(obj);
                                system.debug('Oauth Response: '+res.getbody());
        OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);
                     system.debug(objAuthenticationInfo);   
                System.debug('objAuthenticationInfo'+objAuthenticationInfo);


        Http h1 = new Http();
        HttpRequest req1 = new HttpRequest();
        req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
        req1.setHeader('Content-Type','application/json');
        req1.setHeader('accept','application/json');
        req1.setMethod('GET');
        String strName = 'Hello 3';
        req1.setEndpoint('https://na1.salesforce.com/services/apexrest/Dev_tom/Account1');//URL will be your Salesforce REST API end point where you will do POST,PUT,DELETE orGET
        system.debug('======req1========'+req1);
        HttpResponse res1 = h1.send(req1);
        system.debug('==========res1============'+res1.getBody());

                        JSON2Apex Jsondes = (JSON2Apex)JSON.deserialize(res1.getbody(), JSON2Apex.class);
                                                                System.debug('Jsondes'+Jsondes);

                                                Account A = New Account ();
                                                A.Name = Jsondes.Name;
                                                A.Site = Jsondes.ID;
                                                insert A;

   }   

   global void finish(Database.BatchableContext BC){
   }
}

Hope this will work . 

Thanks
karthik
 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello 

Here  is the bacth apex class for you code .. 

schudle this based on your needs. 
 
global class FetchOtherOrgDetails implements Database.Batchable<sObject>,Database.AllowsCallouts{

   global Database.QueryLocator start(Database.BatchableContext BC){
      return null;
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
    HttpRequest obj = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        String username = 'tom.simmons@hotmail.com';
        String pwd = 'Welcome123rwnYJye3ZaZlxCCq9UJ7sD0AE';
        String reqBody = 'grant_type=password&client_id=3MVG9Y6d_Btp4xp4D3Z8GAwN6wIbOw3vQabsximESJjFoCgpNad5tVtSnQIZN7xAyCekd0tyknn95VTImvYJ2&client_secret=817876896827445493&username='+username+'&password='+pwd;
        obj.setMethod('POST');
        obj.setEndPoint('https://login.salesforce.com/services/oauth2/token');
        obj.setBody(reqBody);
        res = http.send(obj);
                                system.debug('Oauth Response: '+res.getbody());
        OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);
                     system.debug(objAuthenticationInfo);   
                System.debug('objAuthenticationInfo'+objAuthenticationInfo);


        Http h1 = new Http();
        HttpRequest req1 = new HttpRequest();
        req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
        req1.setHeader('Content-Type','application/json');
        req1.setHeader('accept','application/json');
        req1.setMethod('GET');
        String strName = 'Hello 3';
        req1.setEndpoint('https://na1.salesforce.com/services/apexrest/Dev_tom/Account1');//URL will be your Salesforce REST API end point where you will do POST,PUT,DELETE orGET
        system.debug('======req1========'+req1);
        HttpResponse res1 = h1.send(req1);
        system.debug('==========res1============'+res1.getBody());

                        JSON2Apex Jsondes = (JSON2Apex)JSON.deserialize(res1.getbody(), JSON2Apex.class);
                                                                System.debug('Jsondes'+Jsondes);

                                                Account A = New Account ();
                                                A.Name = Jsondes.Name;
                                                A.Site = Jsondes.ID;
                                                insert A;

   }   

   global void finish(Database.BatchableContext BC){
   }
}

Hope this will work. 

Mark Best Answer if its work for you. 

Thanks
karthik
 
Tom SimmonsTom Simmons
Thanks Karthik. but when i`m trying to run this batch, I`m getting an Error " 
First error: Start did not return a valid iterable object.". Any idea why this is happening?
karthikeyan perumalkarthikeyan perumal
Hello, 

i forget to add the query in start method,
use below Code, 
global class FetchOtherOrgDetails implements Database.Batchable<sObject>,Database.AllowsCallouts{

   global Database.QueryLocator start(Database.BatchableContext BC){
     String query = 'Select id  From Account Limi 1';
        return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
    HttpRequest obj = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        String username = 'tom.simmons@hotmail.com';
        String pwd = 'Welcome123rwnYJye3ZaZlxCCq9UJ7sD0AE';
        String reqBody = 'grant_type=password&client_id=3MVG9Y6d_Btp4xp4D3Z8GAwN6wIbOw3vQabsximESJjFoCgpNad5tVtSnQIZN7xAyCekd0tyknn95VTImvYJ2&client_secret=817876896827445493&username='+username+'&password='+pwd;
        obj.setMethod('POST');
        obj.setEndPoint('https://login.salesforce.com/services/oauth2/token');
        obj.setBody(reqBody);
        res = http.send(obj);
                                system.debug('Oauth Response: '+res.getbody());
        OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);
                     system.debug(objAuthenticationInfo);   
                System.debug('objAuthenticationInfo'+objAuthenticationInfo);


        Http h1 = new Http();
        HttpRequest req1 = new HttpRequest();
        req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
        req1.setHeader('Content-Type','application/json');
        req1.setHeader('accept','application/json');
        req1.setMethod('GET');
        String strName = 'Hello 3';
        req1.setEndpoint('https://na1.salesforce.com/services/apexrest/Dev_tom/Account1');//URL will be your Salesforce REST API end point where you will do POST,PUT,DELETE orGET
        system.debug('======req1========'+req1);
        HttpResponse res1 = h1.send(req1);
        system.debug('==========res1============'+res1.getBody());

                        JSON2Apex Jsondes = (JSON2Apex)JSON.deserialize(res1.getbody(), JSON2Apex.class);
                                                                System.debug('Jsondes'+Jsondes);

                                                Account A = New Account ();
                                                A.Name = Jsondes.Name;
                                                A.Site = Jsondes.ID;
                                                insert A;

   }   

   global void finish(Database.BatchableContext BC){
   }
}

Hope this will work . 

Thanks
karthik
 
This was selected as the best answer
Tom SimmonsTom Simmons
Yes, works great now. Thank you very much Karthik.