0

I am passing Python tuple as a SQL parameter when making bulk API Call with simple salesforce library. The script works perfectly fine if my tuple has more than one element in it, however, it gives me IndexError: list index out of range if my tuple has just one element. . My code below:

id_params_tuple = ('0060z000023HFr4AAG',)
sql = "SELECT Id FROM OpportunityLineItem where Opportunity_ID__c IN {}".format(id_params_tuple)
opp_products_raw_data = sf.bulk.OpportunityLineItem.query(sql)
print(opp_products_raw_data)

How can I work around this issue?

Chique_Code
  • 1,130
  • 1
  • 13
  • 29

2 Answers2

-1

You're missing parenthesis in the IN clause. Try this on the second line.

sql = "SELECT Id FROM OpportunityLineItem where Opportunity_ID__c IN ({})".format(id_params_tuple)
Chris
  • 9
  • 1
-1

Try this-

#id_params_tuple = ('0060z000023HFr4AAG',)
id_params_list = ['0060z000023HFr4AAG']
list_of_tuple = [tuple([value]) for value in id_params_list]
cond = (', '.join("'{}'".format(t[0]) for t in list_of_tuple))
sql = "SELECT Id FROM OpportunityLineItem where Opportunity_ID__c IN ({})".format(cond)
opp_products_raw_data = sf.bulk.OpportunityLineItem.query(sql)
print(sql)
Anant
  • 53
  • 8
  • String formatting techniques should not be used to interpolate values into SQL queries. Use the parameter substitution tools provided by the DB-API connector package being used. – snakecharmerb Aug 17 '21 at 05:26