+ Start a Discussion
sushanth s 2sushanth s 2 

How to develop the batch execute method from REST callout?

Hi,

   i'm trying to get the records from external third party application to salesforce case object.
   i'm getting the records from the below REST callout now i'm trying to develop the Aex batch job but i'm stucked at execute() method
  how to develop the execute() based on REST callout

   for the rest callout see below code

  @future (callout=true) global static void getIncident(String subject){
Http http = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
string text = subject;
req.setEndpoint('https://myinstance.service-now.com/api/now/table/incident?sysparm_fields=impact%2Cincident_state%2Cshort_description%2Csys_id%2Ccontact_type&sysparm_limit=2&u_sftype=true');
req.setMethod('GET'); req.setHeader('Content-Type', 'application/json');
String username = 'admin';
String password = 'abcd';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
res = http.send(req); System.debug('jsonrResult :' + res.getBody());
Deserialization.ResponseResult res1= (Deserialization.ResponseResult)JSON.deserialize(res.getBody(), Deserialization.ResponseResult.class); System.debug('Results == :' + res1 );
List<Case> casesToUpsert = new List<Case>();
for(Deserialization d : res1.result ){
Case c = new Case();
c.Priority = d.impact;
c.Status = d.incident_state;
c.Subject = d.short_description;
c.ServiceNowId__c = d.sys_id;
c.Origin = d.contact_type;
casesToUpsert.add(c);
} system.debug('Cases to UPsert ::: ' +casesToUpsert);
if(casesToUpsert.size()>0){
Database.upsert(casesToUpsert,false) ;
}
How to develop my Execute() based on my REST callout class

this is my batach class
global class ServiveNowBatch implements Database.Batchable, Database.AllowsCallouts{
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id, Priority, Status, Subject, ServiceNowId__c, Origin FROM Case WHERE Status=1 LIMIT 50';
return Database.getQueryLocator(query);
} global void execute(Database.BatchableContext BC, List<Case> scope){

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

help me to develop the execute()

Thanks In Advance
Nagendra Prasad ChNagendra Prasad Ch
Hi Sushanth,
Ther is no way we can call @future method from Batch. So instaed of calling that method, copy the same logic into your batch by removing @future atribute(anywat Batch runs asynchronously, @future is not required here).

Finally, your batch class looks like this,
global class ServiveNowBatch implements Database.Batchable, Database.AllowsCallouts{
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id, Priority, Status, Subject, ServiceNowId__c, Origin FROM Case WHERE Status=1 LIMIT 50';
return Database.getQueryLocator(query);
} 

global void execute(Database.BatchableContext BC, List<Case> scope){
List<Case> casesUpsert = new List<Case>();
for(case case : scope){
List<Case> cases = getIncident(case.Subject);
casesUpsert.addAll(cases);
}
if(casesUpsert.size()>0){
Database.upsert(casesUpsert,false) ;
}
}
global void finish(Database.BatchableContext BC){ }

//Modify the @future logic accordingly. Instead of Upserting, method will return to execute method so at the end of execute can upsert all in bulk
public static List<Case> getIncident(String subject){
Http http = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
string text = subject;
req.setEndpoint('https://myinstance.service-now.com/api/now/table/incident?sysparm_fields=impact%2Cincident_state%2Cshort_description%2Csys_id%2Ccontact_type&sysparm_limit=2&u_sftype=true');
req.setMethod('GET'); req.setHeader('Content-Type', 'application/json');
String username = 'admin';
String password = 'abcd';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
res = http.send(req); System.debug('jsonrResult :' + res.getBody());
Deserialization.ResponseResult res1= (Deserialization.ResponseResult)JSON.deserialize(res.getBody(), Deserialization.ResponseResult.class); System.debug('Results == :' + res1 );
List<Case> casesToUpsert = new List<Case>();
for(Deserialization d : res1.result ){
Case c = new Case();
c.Priority = d.impact;
c.Status = d.incident_state;
c.Subject = d.short_description;
c.ServiceNowId__c = d.sys_id;
c.Origin = d.contact_type;
casesToUpsert.add(c);
} system.debug('Cases to UPsert ::: ' +casesToUpsert);
return casesToUpsert;
}

}

Set the batch size to minimum as it consumes more callout resorces and more likely to hit governer limits. Try this and let me know if it works.

Thanks & Regards,
Nagendra Prasad