Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. @future(callout=true)
  2. private static void sendHTTPRequest1(){
  3. String namedCredential = 'NJMPNamedCredential';
  4.  
  5. //Create a Job ----------------------------------------------------------------------------------------
  6. String uri = '/services/data/v45.0/jobs/ingest/';
  7.  
  8. String requestBody =
  9. '{' +
  10. '"operation" : "upsert",' +
  11. '"object" : "Apttus_Config2__PriceListItem__c",' +
  12. '"externalIdFieldName" : "APTS_Ext_ID__c",' +
  13. '"contentType" : "CSV",' +
  14. '"lineEnding" : "CRLF"' +
  15. '}';
  16.  
  17. HttpRequest request = new HttpRequest();
  18. request.setEndpoint('callout:' + namedCredential + uri);
  19. request.setMethod('POST');
  20. request.setHeader('Content-Type', 'application/json;charset=UTF-8');
  21. request.setHeader('Accept', 'application/json');
  22.  
  23. // Set the body as a JSON object
  24. request.setBody(requestBody);
  25.  
  26. System.debug('request: ' + request);
  27.  
  28. Http http = new Http();
  29. HttpResponse response = http.send(request);
  30.  
  31. while (response.getStatusCode() == 302) {
  32. request.setEndpoint(response.getHeader('Location'));
  33. response = new Http().send(request);
  34. }
  35.  
  36. System.debug('response.getBody():' + response.getBody());
  37. //Response is a JSON String that can be accessed like this: - we need the id for the next step.
  38. String responseBody = response.getBody();
  39. Map<String,Object> responseMap = (Map<String, Object>)JSON.deserializeUntyped(responseBody);
  40. System.debug('responseMap:' + responseMap);
  41.  
  42. String jobId = (String)responseMap.get('id');
  43. System.debug('jobId:' + jobId);
  44.  
  45. //Upload CSV ----------------------------------------------------------------------------------------
  46. uri = '/services/data/v45.0/jobs/ingest/' + jobId + '/batches';
  47.  
  48. requestBody =
  49. 'APTS_Ext_Id__c,Apttus_Config2__ProductId__r.APTS_Ext_Id__c,Apttus_Config2__PriceListId__r.APTS_Ext_Id__c' +
  50. 'aG90E00000000QISAY,01t0E000004e2voQBB,aGA0E0000000046WAA';
  51.  
  52. request = new HttpRequest();
  53. request.setEndpoint('callout:' + namedCredential + uri);
  54. request.setMethod('PUT');
  55. request.setHeader('Content-Type', 'text/csv');
  56. request.setHeader('Accept', 'application/json');
  57.  
  58. // Set the body as a JSON object
  59. request.setBody(requestBody);
  60.  
  61. System.debug('request: ' + request);
  62.  
  63. http = new Http();
  64. response = http.send(request);
  65.  
  66. while (response.getStatusCode() == 302) {
  67. request.setEndpoint(response.getHeader('Location'));
  68. response = new Http().send(request);
  69. }
  70.  
  71. System.debug('response.getStatus():' + response.getStatus());
  72. System.debug('response.getBody():' + response.getBody());
  73.  
  74. //Close Job ----------------------------------------------------------------------------------------
  75. uri = '/services/data/v45.0/jobs/ingest/' + jobId;
  76.  
  77. requestBody =
  78. '{' +
  79. '"state" : "UploadComplete"' +
  80. '}';
  81.  
  82. request = new HttpRequest();
  83. request.setEndpoint('callout:' + namedCredential + uri + '?_HttpMethod=PATCH');
  84. request.setMethod('POST');
  85. request.setHeader('Content-Type', 'application/json; charset=UTF-8');
  86. request.setHeader('Accept', 'application/json');
  87.  
  88. // Set the body as a JSON object
  89. request.setBody(requestBody);
  90.  
  91. System.debug('request: ' + request);
  92.  
  93. http = new Http();
  94. response = http.send(request);
  95.  
  96. while (response.getStatusCode() == 302) {
  97. request.setEndpoint(response.getHeader('Location'));
  98. response = new Http().send(request);
  99. }
  100.  
  101. System.debug('response.getStatus():' + response.getStatus());
  102. System.debug('response.getBody():' + response.getBody());
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement