+ Start a Discussion
sushanth s 2sushanth s 2 

guide me to develop a Batch job?

Hi,

      i have developed a apex class for connecting with the third party but now i'm going to write a batch job on that anyone guide me to developa batch job.i have look into some examples but completely not understood.

this is my class :


global with sharing class Service {
    
    global static void getIncident(String subject){  
        
        Http http = new Http();
        HttpRequest req =  new HttpRequest();
        HttpResponse res = new HttpResponse();
         
        string text = subject;
            
        req.setEndpoint('https://dev24994.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&u_sftype=true&sysparm_limit=10');      
        req.setMethod('GET');
        req.setHeader('Content-Type', 'application/json');
        
        String username = 'admin';
        String password = 'xxxxxx';
       
        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 result = (Deserialization.ResponseResult)JSON.deserialize(res.getBody(),     Deserialization.ResponseResult.class);
        System.debug('Results == :' + result );
        
        List<Case> casesToUpsert = new List<Case>();        
        for(Deserialization d : theresult.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) ;
        }
             
    }
    
}

if it is possible develop batch job on that for better understanding

Thanks In Advance
Neha Patil 20Neha Patil 20
Hi Sushanth,

You can simply try to schedule this apex class with the help of an another apex class by implementing the schedulable interface something like this:
 
global class scheduledBatchable implements Schedulable {
   global void execute(SchedulableContext sc) {
      Service b = new Service(); 
      database.executebatch(b);
   }
}
Further you can go to setup-->Apex classes --> click on button--> schedule apex and this will allow you to schedule and execute you batch as and when needed

Let me know if this helps and mark it as best answer if this resolves your query

Regards
Neha