I am newbie to salesforce i am trying to query the salesforce contact object and trying to fetch the all the records from contact object and write it in a csv file. I am trying to do it from below python code
import http.client
import ast
from simple_salesforce import Salesforce
from salesforce_bulk import SalesforceBulk
import re
import time
conn = http.client.HTTPSConnection("my.sandbox.my.salesforce.com")
payload = 'client_id=<my-client-id>&username=<username>&password=<passowrd>&grant_type=password&version=52'
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
conn.request("POST", "/services/oauth2/token", payload, headers)
res = conn.getresponse()
data = res.read()
credentials = ast.literal_eval(data.decode("utf-8"))
bearer_token = credentials["access_token"]
instance_url = credentials["instance_url"]
bulk = SalesforceBulk(sessionId=bearer_token, host=instance_url)
sf = Salesforce(instance_url=instance_url, session_id=bearer_token)
desc = sf.Contact.describe()
field_names = [field["name"] for field in desc["fields"]]
soql_query = f"SELECT {', '.join(field_names)} FROM Contact"
job = bulk.create_query_job("Contact", contentType="CSV", pk_chunking=True)
results = sf.query_all(soql_query)
batch = bulk.query(job, soql_query)
while not bulk.is_batch_done(batch):
time.sleep(10)
w = []
# Get the results
for result in bulk.get_all_results_for_query_batch(batch):
reader = unicodecsv.DictReader(result, encoding='utf-8')
for row in reader:
w.append(row)
If i am trying to run above code, then it is giving below error
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/salesforce_bulk/salesforce_bulk.py", line 435, in is_batch_done
raise BulkBatchFailed(job_id, batch_id, status.get('stateMessage'), batch_state)
salesforce_bulk.salesforce_bulk.BulkBatchFailed: Batch 75179000005QyH4AAK of job None failed: InvalidBatch : Failed to process query: INVALID_FIELD: PhotoUrl, Jigsaw, JigsawContactId, Pronouns, GenderIdentity, cvup2__AdditionalReportsTo__c ^ ERROR at Row:1:Column:951 No such column 'Pronouns' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
What i understood from above error is the custom field need to be appended with ‘__c’ issue and i tried even trying to add the ‘__c’ for custom fields using below line but still gives same above error
custom_field_names = [f"{field}__c" if not re.match(r".*__c$", field) else field for field in field_names]
But when i try to run below query it is running successfully and fetching the data
soql_query = “SELECT Id, LastName FROM Contact LIMIT 10”
I tried even below query also that is also not working
SELECT FIELDS(ALL) FROM Contacts
But it is giving below error and i tried to use bulk version 52 still getting same error
salesforce_bulk.salesforce_bulk.BulkBatchFailed: Batch 75179000005QyDRAA0 of job None failed: InvalidBatch : Failed to process query: UNSUPPORTED_API_VERSION: SELECT FIELDS(ALL) FROM Contact ^ ERROR at Row:1:Column:8 The SOQL FIELDS function is not supported in this version of the API
Please help me with how can i get all fields from the object and store it as a csv file or please point out where i am doing wrong here