• Zack Bergeron
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Hello everyone,

I am having an issue performing a bulk query when PK Chunking is enabled. This is the exact error I receive: "InternalServerError : PKChunking failed. InvalidBatch : Wrong content-type for batch (text/csv), job is of type: application/xml".

Below I provided the sample C# .NET code I'm running where I receive the error. 

It should be noted that the job and batch are being created successfully but fails when salesforce runs the batch.
Also, I've found that this error only occurs when the PK Chunking header is enabled. When disabled, the batch will run successfully and return the results I requested. 

Additional notes. I'm performing a simple query in sandbox on a custom object.

Does anyone know what I may be doing wrong?

Thanks!




public static void performBulkQuery(String objectAPIName, String soqlQuery)
{
    //create new job
    JobWrapper job = createJob("query", objectAPIName, 500);

    //create new batch
    BatchWrapper batch = createBatch(job.Id, soqlQuery);
}

public static JobWrapper createJob(String operation, String objectName, int pkChunkingSize)
{
    String jobRequestXML =
        @"<?xml version=""1.0"" encoding=""UTF-8""?>
         <jobInfo xmlns=""http://www.force.com/2009/06/asyncapi/dataload"">
           <operation>{0}</operation>
           <object>{1}</object>
           <contentType>XML</contentType>
         </jobInfo>";

    jobRequestXML = String.Format(jobRequestXML, operation, objectName);

    String requestUrl = "https://" + MainProgram.SFInstance + ".salesforce.com/services/async/36.0/job";

    WebClient wc = new WebClient();
    wc.Encoding = Encoding.UTF8;
    wc.Headers.Add("X-SFDC-Session: " + MainProgram.loginResult.sessionId);
    wc.Headers.Add("Content-Type: application/xml");
    String chunkSize = pkChunkingSize.ToString();
    wc.Headers.Add("Sforce-Enable-PKChunking: chunkSize=" + chunkSize);

    String resultXML = invokeRestAPI(requestUrl, jobRequestXML, wc);

    return JobWrapper.parseJobResponse(resultXML);
}

public static BatchWrapper createBatch(String jobId, String batchRequestXML)
{
    String requestUrl = "https://" + MainProgram.SFInstance + ".salesforce.com/services/async/36.0/job/" + jobId + "/batch";

    WebClient wc = new WebClient();
    wc.Encoding = Encoding.UTF8;
    wc.Headers.Add("X-SFDC-Session: " + MainProgram.loginResult.sessionId);
    wc.Headers.Add("Content-Type: application/xml");

    String resultXML = invokeRestAPI(requestUrl, batchRequestXML, wc);

    return BatchWrapper.parseBatchResponse(resultXML);
}

private static String invokeRestAPI(String requestUrl, String requestXML, WebClient wc)
{
    try
    {
        return wc.UploadString(requestUrl, "Post", requestXML);
    }
    catch (WebException webEx)
    {
        String error = String.Empty;

        if (webEx.Response != null)
        {
            using (var errorResponse = (HttpWebResponse)webEx.Response)
            {
                using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                {
                    error = reader.ReadToEnd();
                }
            }
        }

        throw;
    }
}