function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zachariah RosenbergZachariah Rosenberg 

Bulk-API: How to add a batch with both CSV data and a query?

Hello,

I'm trying to use Salesforce's bulk API to run a query on some CSV data. Here is the desired result:

In SF, we have a field product.serial__c. I have a CSV with a bunch of serial numbers that I would like to pull other fields from, i.e.
SELECT product.name, product.price__c FROM product where product.serial__c IN (serial_csv)
So I'd like to add a batch, filled with this CSV data and run a query with it.

My question is how the add batch API call should be. Here is my function in python
#Code thanks to http://www.wadewegner.com/2014/04/update-records-with-python-and-the-salesforce-bulk-api/
def addBatch(instance, session_id, job_id, objects):

	request = u"""<?xml version="1.0" encoding="UTF-8"?>
  					<sObjects xmlns="http://www.force.com/2009/06/asyncapi/dataload">
    				 	""" + objects + """
  					</sObjects>"""

  	#encoded_request = request.encode('utf-8')
  	url = "https://" + instance + ".salesforce.com/services/async/32.0/job/" + job_id + "/batch"
  	headers = {"X-SFDC-Session": session_id, "Content-Type": "text/csv; charset=UTF-8"}

  	response = requests.post(url = url, headers = headers, data = request, verify = False)
  	return unicode(response.text)
Currently, I'm placing all my csv data in the objects parameter, i.e.:
csv_data = "0000,0001,0002,0003,0004,...,1111"
response = addBatch(instance, session_id, job_id, csv_data)

Is this incorrect?

Where do I put the query? All the docs on SF seem to point that I would put it in the 'objects' parameter in my function. Then where would my csv data go?

I'm sure I've completely confused how to use this API...

Thank you all!

Best,
Zac
ShashankShashank (Salesforce Developers) 
You probably need to create the csv in an acceptable format, i.e., including a column for the csv. https://www.salesforce.com/us/developer/docs/api_asynch/Content/asynch_api_mapping.htm