Guest User

Untitled

a guest
Oct 1st, 2018
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. global class ServiveNowBatch implements Database.Batchable<sObject>, Database.AllowsCallouts{
  2.  
  3. global Database.QueryLocator start(Database.BatchableContext BC){
  4.  
  5. String query = 'SELECT Id, Priority, Status, Subject, ServiceNowId__c, Origin FROM Case WHERE Status=1 LIMIT 50';
  6.  
  7. return Database.getQueryLocator(query);
  8. }
  9.  
  10. global void execute(Database.BatchableContext BC, List<Case> scope){
  11.  
  12. }
  13.  
  14. global void finish(Database.BatchableContext BC){
  15.  
  16. }
  17. }
  18.  
  19. @future (callout=true)
  20. global static void getIncident(String subject){
  21.  
  22. Http http = new Http();
  23. HttpRequest req = new HttpRequest();
  24. HttpResponse res = new HttpResponse();
  25.  
  26. string text = subject;
  27. 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');
  28.  
  29. req.setMethod('GET');
  30. req.setHeader('Content-Type', 'application/json');
  31.  
  32. String username = 'admin';
  33. String password = 'abcd';
  34. Blob headerValue = Blob.valueOf(username + ':' + password);
  35. String authorizationHeader = 'BASIC ' +
  36. EncodingUtil.base64Encode(headerValue);
  37. req.setHeader('Authorization', authorizationHeader);
  38. res = http.send(req);
  39. System.debug('jsonrResult :' + res.getBody());
  40. Deserialization.ResponseResult res1= (Deserialization.ResponseResult)JSON.deserialize(res.getBody(), Deserialization.ResponseResult.class);
  41. System.debug('Results == :' + res1 );
  42.  
  43. List<Case> casesToUpsert = new List<Case>();
  44. for(Deserialization d : res1.result ){
  45.  
  46. Case c = new Case();
  47. c.Priority = d.impact;
  48. c.Status = d.incident_state;
  49. c.Subject = d.short_description;
  50. c.ServiceNowId__c = d.sys_id;
  51. c.Origin = d.contact_type;
  52.  
  53. casesToUpsert.add(c);
  54.  
  55. }
  56. system.debug('Cases to UPsert ::: ' +casesToUpsert);
  57.  
  58. if(casesToUpsert.size()>0){
  59. Database.upsert(casesToUpsert,false) ;
  60. }
  61.  
  62. global Database.QueryLocator start(Database.BatchableContext BC){
  63. String query = 'SELECT Id, Priority, Status, Subject, ServiceNowId__c, Origin '+
  64. 'FROM Case '+
  65. 'WHERE **condition**'+//your condition to get records
  66. 'LIMIT 50';//Add if you need to limit the records
  67. return Database.getQueryLocator(query);
  68. }
  69.  
  70. String query = 'SELECT Id, Priority, Status, Subject, ServiceNowId__c, Origin '+
  71. 'FROM Case '+
  72. 'WHERE **condition**'+//your condition to get records
  73. 'LIMIT 50';//Add if you need to limit the records
  74. MyBatchClass batch = new MyBatchClass(query);
  75. database.executeBatch(batch);
  76.  
  77. global class MyBatchClass implements
  78. Database.Batchable<sObject>, Database.Stateful,Database.AllowsCallout{
  79.  
  80. global final String query;
  81. global integer summary;
  82.  
  83. global MyBatchClass(String q){
  84. query = q;
  85. summary = 0;
  86. }
  87.  
  88. global Database.QueryLocator start(Database.BatchableContext BC){
  89. return Database.getQueryLocator(query);
  90. }
  91.  
  92. global void execute( Database.BatchableContext BC, List<sObject> scope){
  93. for(sObject s : scope){
  94. summary = Integer.valueOf(s.get('total__c'))+summary;
  95. }
  96. }
  97.  
  98. global void finish(Database.BatchableContext BC){
  99. }
  100. }
Add Comment
Please, Sign In to add comment