+ Start a Discussion
sushanth s 2sushanth s 2 

Batchjob from apex REST class?

Hi All,

       i have developed a REST class for to integrating salesforce with servicenow.now i'm getting the incidents created in servicenow into salesforce cases that's also working fine "BUT HOW TO WRITE A BATCH JOB" on Salesforce Cases and call it from after insert trigger.

in my apex rest class i'm calling all the incidents of type sftype from servicenow.
i'm encoding in endpoint url:


here i'm calling all the incidents from servicenow into salsforce cases.

This is my REST class
global with sharing class SfService {
     @future (callout=true) 
     global static void getIncident(String subject){  
        Http http = new Http();
        HttpRequest req =  new HttpRequest();
        HttpResponse res = new HttpResponse();
        string text = subject;  
       //getting all the newly created incidents since last 20 mins of Type is SfType from serviceNow endpoint 
        req.setHeader('Content-Type', 'application/json'); 
        String username = 'admin';
        String password = 'abcd';
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'BASIC ' +
        req.setHeader('Authorization', authorizationHeader);  
        res = http.send(req);
        System.debug('jsonrResult :' + res.getBody());       
        Deserialization.ResponseResult theresult1 = (Deserialization.ResponseResult)JSON.deserialize(res.getBody(),  Deserialization.ResponseResult.class);
        System.debug('Results == :' + theresult1 ); 
        List<Case> casesToUpsert = new List<Case>();        
        for(Deserialization d : theresult1.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;
        system.debug('Cases to UPsert ::: ' +casesToUpsert);
            Database.upsert(casesToUpsert,false) ;
Anyone help to develop the apex batch job on Cases  and call it from after insert cases so that if that batch job is running itpulling all the incidents from service now of type sftype into salesforce Cases.

Thanks In Advance
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Sushanth,

May I request you to please refer the below link for reference from stack exchange community. I hope it will be helpful.

Best Regards
Rahul Kumar