Baxter
Baxter

Reputation: 2456

Python beatbox salesforce external Id upsert

I'm trying to upsert a record via the salesforce Beatbox python client the upsert operation seems to work fine but I can't quite work out how to specify an externalid as a foreign key:

Attempting to upsert with:

consolidatedToInsert = []

for id,ce in ConsolidatedEbills.items():
    consolidatedToInsert.append(
        {
            'type':'consolidated_ebill__c',
            'Account__r':{'type':'Account','ETL_Natural_Key__c':ce['CLASS_REFERENCE']},
            'ETL_Natural_Key__c':ce['ISSUE_UNIQUE_ID']
        }
    )

print consolidatedToInsert[0]

pc.login('USERNAME', 'TOTALLYREALPASSWORD')
ret = pc.upsert('ETL_Natural_Key__c',consolidatedToInsert[0])
print ret

gives the error:

'The external foreign key reference does not reference a valid entity: Account__r'

[{'isCreated': False, 'errors': [{'fields': [], 'message': 'The external foreign key reference does not reference a valid entity: Account__r', 'statusCode': 'INVALID_FIEL D'}], 'id': '', 'success': False, 'created': False}]

The soap examples and the specificity of the error text seem to indicate that it's possible but I can find little in the documentation about inserting with external ids.


On a closer look I'm not sure if this is possible at all, a totally mangled key to Account__r seems to pass silently as if it's not even being targeted for XML translation, I'd love to be wrong though.


A quick change to pythonclient.py 422:0:

     for k,v in field_dict.items():
         if v is None:
             fieldsToNull.append(k)
             field_dict[k] = []
         if k.endswith('__r') and isinstance(v,dict):
             pass
         elif hasattr(v,'__iter__'):
             if len(v) == 0:
                 fieldsToNull.append(k)
             else:
                 field_dict[k] = ";".join(v)

and another to __beatbox.py 375:0

        for fn in sObjects.keys():
            if (fn != 'type'):
                if (isinstance(sObjects[fn],dict)):
                    self.writeSObjects(s, sObjects[fn], fn)
                else:
                    s.writeStringElement(_sobjectNs, fn, sObjects[fn])

and it works like some dark magic.

Upvotes: 3

Views: 728

Answers (1)

superfell
superfell

Reputation: 19040

Currently Beatbox doesn't support serializing nested dictionaries like this, which is needed for the externalId resolution you're trying to do. (If you look at the generated request, you can see that the nested dictionary is just serialized as a string)).

Upvotes: 4

Related Questions