Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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){
- }
- }
- @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) ;
- }
- 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);
- }
- 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);
- 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){
- }
- }
Add Comment
Please, Sign In to add comment