0

i'm getting the records from third party in sfdc cases. for that i have developed a rest callout.but i'm stuched at for calling third party records i invoked in endpoint url in my rest callout like this

req.setEndpoint('https://owninstance.service-now.com/api/now/table/incident?sysparm_fields=impact%2Cincident_state%2Csys_updated_onONLast%2520minute%40javascript%3Ags.minutesAgoStart(30)%40javascript%3Ags.minutesAgoEnd(0)%2Cshort_description%2Csys_id%2Ccontact_type&sysparm_limit=2&u_sftype=true');

i'm stucked at querying the records in start() in batch class or how to get the third party record into salesforce.

global class ServiveNowBatch implements Database.Batchable<sObject>, 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){

    }
}

this my REST class :

@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

Thanks,

  • What is there in query? Also how do you invoke batch? – Reshma Mar 30 '17 at 3:58
  • my doubt is that only because i'm getting the records in url format in above mentioned endpoint. – shanker Mar 30 '17 at 3:59
0

You can go through how to use batch class.

Either you can specify query within start() method of batch itself like:

global Database.QueryLocator start(Database.BatchableContext BC){
     String query = 'SELECT Id, Priority, Status, Subject, ServiceNowId__c, Origin '+
                    'FROM Case '+
                    'WHERE **condition**'+//your condition to get records
                    'LIMIT 50';//Add if you need to limit the records
     return Database.getQueryLocator(query);
}

Or you can pass this query in batch constructor when you invoke batch:

String query = 'SELECT Id, Priority, Status, Subject, ServiceNowId__c, Origin '+
                        'FROM Case '+
                        'WHERE **condition**'+//your condition to get records
                        'LIMIT 50';//Add if you need to limit the records
MyBatchClass batch = new MyBatchClass(query); 
database.executeBatch(batch);

You need to update your batch constructor with a string parameter.

global class MyBatchClass implements 
    Database.Batchable<sObject>, Database.Stateful,Database.AllowsCallout{

    global final String query;
    global integer summary;

    global MyBatchClass(String q){
        query = q;
        summary = 0;
    }

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

    global void execute( Database.BatchableContext BC, List<sObject> scope){
        for(sObject s : scope){
            summary = Integer.valueOf(s.get('total__c'))+summary;
        }
    }

    global void finish(Database.BatchableContext BC){
    }
}
  • global unnecessary except in managed packages. You might want to add bits about where the callouts would go as that was the heart of the OP – cropredy Apr 3 '17 at 6:13

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.