0

I am doing a POC on the BULK API and have run through this trailhead: https://trailhead.salesforce.com/en/content/learn/modules/api_basics/api_basics_bulk where you call the BULK API through WorkBench.

I am now doing the same in an Apex method, which seems to be working until I get to the Close Job Request. Response Status: Bad Request, Response Body:[{"errorCode":"BULK_API_ERROR","message":"Cannot specify query parameters on this resource"}]

Note that I am using a workaround to call the PATCH method as suggested here: https://success.salesforce.com/ideaView?id=0873A000000PST0QAO

This is the method - I know this needs to be cleaned up, but just want to see it working:

@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());
}

The Debug Statements are as follows: enter image description here

Hope you can help! :)

  • just to clarify - are you saying that when you use request.setMethod('PATCH'); for closing the job you are getting error? – Sree May 1 at 9:29
  • Hi @Sree, Salesforce is not recognizing the PATCH verb, so I am using a workaround: '?_HttpMethod=PATCH' added to the URI and then using the POST method. The error I am getting is when sending the last HTTP Request under Close Job. – MR.M. May 1 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.