3

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

shartshooter
  • 1,761
  • 5
  • 19
  • 40

1 Answers1

3

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. e.g.

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

hynekcer
  • 14,942
  • 6
  • 61
  • 99
superfell
  • 18,780
  • 4
  • 59
  • 81
  • can see you are a salesforce guy which is good as some of us are newbies with it. I am reasonably sure the `body = ""` line at the top of your code can go away, the variable is being replaced underneath. You only do that if you are appending to a list or string. – cardamom Aug 15 '19 at 14:42