5

Dynamically add an object to the bulk query job for Simple_Salesforce.

I'm not sure how to pass a variable to sf.bulk."Object".query.

I would like to be able to pass an object say "Account" to the definition and it does the bulk query sf.bulk.Account.query("SOQL...")

sf = Salesforce(username=sf_username, password=sf_password, security_token=sf_token)

def SOQL(table):
    qryResult = sf.bulk.`table`.query('SELECT Id FROM ' + table)

SOQL("Account")

I would like python to translate this to sf.bulk.Account.query("SELECT Id FROM Account")

bkyada
  • 332
  • 1
  • 9
Bryan
  • 77
  • 1
  • 8
  • This looks like a minimal example of what you're trying to do so, nice. But, might be able to help you more if you add a bit more info as to why you want to do this? –  May 12 '19 at 01:41

1 Answers1

7

You can directly call sf.bulk's __getattr__ method:

sf = Salesforce(username=sf_username, password=sf_password, security_token=sf_token)

def SOQL(table):
    return sf.bulk.__getattr__(table).query('SELECT Id FROM ' + table)

result = SOQL("Account")

I added a return cause I figured you'll want the result (I realize this is probably a minimal example but still).

Why this works:

Basically, behind the scenes, when you call sf.bulk.Account, python is calling sf.bulk.__getattr__("Account") for you. (that's a long story made short; dig more into python and into the linked source code for more)

Community
  • 1
  • 1
  • Why use the `__getattr__` method instead of [`getattr()`](https://docs.python.org/3/library/functions.html#getattr)? – Adam Matan May 12 '22 at 11:56