Last active
August 9, 2024 16:01
-
-
Save wadewegner/df609a495df2e4bd7a07 to your computer and use it in GitHub Desktop.
Upload attachment to Salesforce using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import base64 | |
import json | |
from simple_salesforce import Salesforce | |
userName = '' | |
password = '' | |
securityToken = '' | |
instance = '' | |
sf = Salesforce(username=userName, password=password, security_token=securityToken) | |
sessionId = sf.session_id | |
account = sf.query("SELECT Id, Name FROM Account LIMIT 1") | |
accountId = account["records"][0]["Id"] | |
body = "" | |
with open("text.txt", "r") as f: | |
body = base64.b64encode(f.read()) | |
response = requests.post('https://%s.salesforce.com/services/data/v29.0/sobjects/Attachment/' % instance, | |
headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer %s' % sessionId }, | |
data = json.dumps({ | |
'ParentId': accountId, | |
'Name': 'rest_test.txt', | |
'body': body | |
}) | |
) | |
print response.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import beatbox | |
import base64 | |
userName = '' | |
securityToken = '' | |
password = '' + securityToken | |
svc = beatbox.PythonClient() | |
svc.login(userName, password) | |
body = "" | |
with open("text.txt", "rb") as f: | |
body = base64.b64encode(f.read()) | |
res = svc.query("SELECT Id, Name FROM Account LIMIT 1") | |
accountId = res[0].Id | |
print res[0].Name | |
update_dict = { | |
'type' : 'Attachment', | |
'ParentId' : accountId, | |
'Name' : 'text.txt', | |
'Body' : body | |
} | |
results = svc.create(update_dict) | |
print results |
Thanks, very helpful - if anyone ends up here and is looking for similar functionality for files, a more updated and feature-rich option vs. attachments:
from simple_salesforce import Salesforce
userName = ''
password = ''
securityToken = ''
instance = ''
sf = Salesforce(username=userName, password=password, security_token=securityToken)
sessionId = sf.session_id
account = sf.query("SELECT Id, Name FROM Account LIMIT 1")
account_id = account["records"][0]["Id"]
file = "text.txt"
with open(file, "rb") as f:
body = base64.b64encode(f.read()).decode("utf-8")
content_version_result = sf.ContentVersion.create(
dict(
title=file,
PathOnClient=file,
VersionData=body,
)
)
content_version = sf.ContentVersion.get(content_version_result["id"])
document_link = sf.ContentDocumentLink.create(
dict(
ContentDocumentId=content_version["ContentDocumentId"],
LinkedEntityId=account_id,
Visbility="AllUsers",
)
)
print(
f"Linked {document_link["id"]} from file {content_version['ContentDocumentId']} to record {account_id}",
)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IMHO,
Should be
for python3 (ie. b64encode method returns a byte in python3, so convert that into a string).