You need to sign in to do that
Don't have an account?
Tom 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...
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; } }
i forget to add the query in start method,
use below Code,
Hope this will work .
Thanks
karthik
All Answers
Here is the bacth apex class for you code ..
schudle this based on your needs.
Hope this will work.
Mark Best Answer if its work for you.
Thanks
karthik
First error: Start did not return a valid iterable object.". Any idea why this is happening?
i forget to add the query in start method,
use below Code,
Hope this will work .
Thanks
karthik