Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @future(callout=true)
- private static void sendHTTPRequest1(){
- String namedCredential = 'NJMPNamedCredential';
- //Create a Job ----------------------------------------------------------------------------------------
- String uri = '/services/data/v45.0/jobs/ingest/';
- String requestBody =
- '{' +
- '"operation" : "upsert",' +
- '"object" : "Apttus_Config2__PriceListItem__c",' +
- '"externalIdFieldName" : "APTS_Ext_ID__c",' +
- '"contentType" : "CSV",' +
- '"lineEnding" : "CRLF"' +
- '}';
- HttpRequest request = new HttpRequest();
- request.setEndpoint('callout:' + namedCredential + uri);
- request.setMethod('POST');
- request.setHeader('Content-Type', 'application/json;charset=UTF-8');
- request.setHeader('Accept', 'application/json');
- // Set the body as a JSON object
- request.setBody(requestBody);
- System.debug('request: ' + request);
- Http http = new Http();
- HttpResponse response = http.send(request);
- while (response.getStatusCode() == 302) {
- request.setEndpoint(response.getHeader('Location'));
- response = new Http().send(request);
- }
- System.debug('response.getBody():' + response.getBody());
- //Response is a JSON String that can be accessed like this: - we need the id for the next step.
- String responseBody = response.getBody();
- Map<String,Object> responseMap = (Map<String, Object>)JSON.deserializeUntyped(responseBody);
- System.debug('responseMap:' + responseMap);
- String jobId = (String)responseMap.get('id');
- System.debug('jobId:' + jobId);
- //Upload CSV ----------------------------------------------------------------------------------------
- uri = '/services/data/v45.0/jobs/ingest/' + jobId + '/batches';
- requestBody =
- 'APTS_Ext_Id__c,Apttus_Config2__ProductId__r.APTS_Ext_Id__c,Apttus_Config2__PriceListId__r.APTS_Ext_Id__c' +
- 'aG90E00000000QISAY,01t0E000004e2voQBB,aGA0E0000000046WAA';
- request = new HttpRequest();
- request.setEndpoint('callout:' + namedCredential + uri);
- request.setMethod('PUT');
- request.setHeader('Content-Type', 'text/csv');
- request.setHeader('Accept', 'application/json');
- // Set the body as a JSON object
- request.setBody(requestBody);
- System.debug('request: ' + request);
- http = new Http();
- response = http.send(request);
- while (response.getStatusCode() == 302) {
- request.setEndpoint(response.getHeader('Location'));
- response = new Http().send(request);
- }
- System.debug('response.getStatus():' + response.getStatus());
- System.debug('response.getBody():' + response.getBody());
- //Close Job ----------------------------------------------------------------------------------------
- uri = '/services/data/v45.0/jobs/ingest/' + jobId;
- requestBody =
- '{' +
- '"state" : "UploadComplete"' +
- '}';
- request = new HttpRequest();
- request.setEndpoint('callout:' + namedCredential + uri + '?_HttpMethod=PATCH');
- request.setMethod('POST');
- request.setHeader('Content-Type', 'application/json; charset=UTF-8');
- request.setHeader('Accept', 'application/json');
- // Set the body as a JSON object
- request.setBody(requestBody);
- System.debug('request: ' + request);
- http = new Http();
- response = http.send(request);
- while (response.getStatusCode() == 302) {
- request.setEndpoint(response.getHeader('Location'));
- response = new Http().send(request);
- }
- System.debug('response.getStatus():' + response.getStatus());
- System.debug('response.getBody():' + response.getBody());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement