简体   繁体   中英

Uploading Attachments to Salesforce API via Beatbox, Python

I'm uploading documents to Salesforce using beatbox and python and the files are attaching correctly but the data contained within the files gets completely corrupted.

def Send_File():
    import beatbox
    svc = beatbox.Client()  # instantiate the object
    svc.login(login1, pw1)  # login using your sf credentials

    update_dict = {
        'type':'Attachment',
        'ParentId': accountid,
        'Name': 'untitled.txt',
        'body':'/Users/My_Files/untitled.txt',
            }
    results2 = svc.create(update_dict)
    print results2

output is:

    00Pi0000005ek6gEAAtrue

So things are coming through well, but when I go to the salesforce record 00Pi0000005ek6gEAA and view the file the contents of the file are:

   ˝KÆœ  Wøä ï‡Îä˜øHÅCj÷øaÎ0j∑ø∫{b∂Wù

I have no clue what's causing the issue and I can't find any situations where this has happened to other people

Link to SFDC Documentation on uploads

the 'body' value in the dictionary should be the base64 encoded contents of the file, not the file name. you need to read and encode the file contents yourself. eg

body = ""
with open("/Users/My_Files/untitled.txt", "rb") as f:
    body = f.read().encode("base64")

update_dict = {
    'type' : 'Attachement'
    'ParentId' : accountId,
    'Name' : 'untitled.txt',
    'Body' : body }

...

Docs about Attachment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM