Python – Convert Salesforce results of type Instance to Dictionary

It was quite a hassle converting a salesfoce result object (type instance) into a dictionary. I tried iterating through each item and converting into a dictionary – but didn’t work. Finally, I used the getattr method to get child attributes of the class. One child attribute was of type instance so I had to access it using the dot notation. Below is a sample data structure and the code to parse it. The source data was a List of Instances that has . The final data is a list of Dictionaries (containing a sub-dictionary for Owner).

''' # Sample Data
[(Account){
   Id = "xxxxx01"
   Customer_Number__c = "21425"
   Industry = "Industrial/Manf"
   Name = "Cust A"
   Owner = 
      (User){
         Id = None
         Email = "fn1.ln1@email.com"
         LastName = "LN1"
         Name = "FN1 LN1"
      }
   OwnerId = "yyy01"
 }, (Account){
   Id = "xxxx01"
   Customer_Number__c = "21426"
   Industry = "Government"
   Name = "Cust B"
   Owner = 
      (User){
         Id = None
         Email = "fn2.ln2@email.com"
         LastName = "LN2"
         Name = "FN2 LN2"
      }
   OwnerId = "yyy02"
 }, (Account){
   Id = "xxxxx03"
   Customer_Number__c = "21427"
   Industry = "Education"
   Name = "Cust C"
   Owner = 
      (User){
         Id = None
         Email = "fn3.ln3@email.com"
         LastName = "LN3"
         Name = "FN3 LN3"
      }
   OwnerId = "yyy03"
 }
 ]
'''

sfClient = SforceEnterpriseClient(SF_WSDL_XML)
sfClient.login(SF_LOGIN, SF_PASSWORD, SF_TOKEN)
data = []
strQuery = "SELECT Name, OwnerId, Account.ID, Owner.Name, Owner.Email, Owner.LastName, Tech_Acct_Manager__c, Customer_Number__c, Industry FROM Account"
if (sfid is not None):
   strQuery += " WHERE Customer_Number__c = '%s'" % (sfid)
list_result = sf_queryMore(sfClient, strQuery)
len_list = len(list_result)
for i in range(0, len_list):
   dict_account = {}
   dict_account['Name'] = getattr(list_result[i], 'Name', "").encode('utf8')
   dict_account['OwnerId'] = getattr(list_result[i], 'OwnerId', "").encode('utf8')
   dict_account['Id'] = getattr(list_result[i], 'Id', "").encode('utf8')
   dict_account['Customer_Number__c'] = getattr(list_result[i], 'Customer_Number__c', "").encode('utf8')
   dict_account['Industry'] = getattr(list_result[i], 'Industry', "").encode('utf8')
   dict_owner = {}
   dict_owner['Id'] = None if list_result[i].Owner.Id is None else list_result[i].Owner.Id.encode('utf8')
   dict_owner['Name'] = None if list_result[i].Owner.Name is None else list_result[i].Owner.Name.encode('utf8')
   dict_owner['LastName'] = None if list_result[i].Owner.LastName is None else list_result[i].Owner.LastName.encode('utf8')
   dict_owner['Email'] = None if list_result[i].Owner.Email is None else list_result[i].Owner.Email.encode('utf8')
   dict_account['Owner'] = dict_owner
   data.append(dict_account)
print data
Advertisements

One thought on “Python – Convert Salesforce results of type Instance to Dictionary

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s