0

I'm having an issue getting what I think is a simple script working and I think it's just me not knowing how to get a variable working.

i'm working on a salesforce script that grabs a list of objects, then looks through the list of objects to get all fields on the table.

this query in python works perfectly for giving me my list of objects that I've pulled into the DB

query = ("SELECT obj_name FROM syncsfobjects")
cursor.execute(query)

i then loop through these records

for x in cursor:

now this is where my issue lies I want to use the obj_name that comes in from my query within the next statement

for xy in sf.%obj_name%.describe()["field"]:

what i'm having massive issues with is getting the obj name into this simple-salesforce query.

if I create a string it works fine

objectname = str(x)
sfquery = 'sf. %s .describe()["fields"]' % objectname

but then when I use the sfquery for my next loop all the loop does it run through each letter within the string instead of run the sf connection command.

any I just missing something simple?

cheers dan

5
  • Could you show what the data you get from the query looks like ? Commented Oct 7, 2015 at 11:55
  • I am not an SQL guru, but isn't SELECT obj_name FROM syncsfobjects only selecting one field? Commented Oct 7, 2015 at 11:57
  • the sql bit isn't the issue as I can do: objectname = str(x) print (objectname) and i get the right output - the issue seems to be recreating the simple-salesforce query sf.Contact.describe()["fields"] - where contact is the name pulled from the db - because this isn't a string i can't seem to get my variable working Commented Oct 7, 2015 at 11:58
  • do you mean cursor['objectname'] as Contact? Commented Oct 7, 2015 at 12:02
  • When you execute in this fashion, the records returned are list of tuples. When you do str(tuple) where there is only one element in tuple it simply converts to string. Commented Oct 7, 2015 at 12:04

1 Answer 1

2
for xy in sf.%obj_name%.describe()["field"]:

Python doesn't let you do "percent substitution" outside of strings, but you can still access attributes if all you have are their names:

for xy in getattr(sf, objectname).describe()["field"]:
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.