晓天蔡
晓天蔡

Reputation: 25

query a list of ids in Python via simple-salesforce

I've been trying to query a list of ids from salesforce in Python, here is my code:

def query_task():
workbook = xlrd.open_workbook(r'C:\Users\jet.cai\Desktop\TaskContact.xlsx')
sheet = workbook.sheet_by_index(0)

for rowx in range(sheet.nrows):
    task_id = sheet.row_values(rowx, start_colx=0, end_colx=None)[0]
    new_task_id = "'" + task_id + "'"
    task_result = sf.query(format_soql("SELECT Id, WhoId FROM Task WHERE Id = {new_task_id}", new_task_id))
    print(task_result)

As you can tell, the task_id comes from excel file, I iterate the first column and reformat it so that the soql query can be executed, however, the error message:

  File "C:\Users\jet.cai\AppData\Local\Programs\Python\Python38-32\lib\string.py", line 229, in get_value
return kwargs[key]
  KeyError: 'new_task_id'

I can understand this format_soql function can only take in **kwargs. Well, can anybody help me out to get the right result?

Thanks

Upvotes: 1

Views: 1174

Answers (2)

ascendedcrow
ascendedcrow

Reputation: 53

you should be able to use sf.format_soql, or have a look here: https://www.oktana.com/python-and-salesforce/

Upvotes: 0

cs95
cs95

Reputation: 402483

Try passing your ID as a keyword arg:

task_result = sf.query(format_soql(
    "SELECT Id, WhoId FROM Task WHERE Id = {task_id}", 
    task_id=task_id))
    

From the docs, it appears the quoting is done automatically, so you can skip the new_task_id = "'" + task_id + "'" step.

Upvotes: 1

Related Questions