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
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:

https://myInstance-now.com/api/now/table/incident?sysparm_fields=impact%2Cincident_state%2Cshort_description%2Csys_id%2Ccontact_type&sysparm_limit=2&u_sftype=true');         

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.setEndpoint('https://dev24994.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 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;
                
                casesToUpsert.add(c);
               
        }
        system.debug('Cases to UPsert ::: ' +casesToUpsert);
      
        if(casesToUpsert.size()>0){
            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