[英]How to get files from Salesforce using Python
I am using Python/Beatbox to access Salesforce cases.我正在使用 Python/Beatbox 访问 Salesforce 案例。
service = beatbox.PythonClient() # instantiate the object
service.login(...) # login using your sf credentials
query_result = service.query("SELECT Id, AccountId, CaseNumber FROM Case WHERE Id='xyz' ")
I'm interested in a specific case:我对一个特定的案例感兴趣:
print query_result[0].Id
Get attachments...获取附件...
att_result = service.query("SELECT Id, ContentType, Name FROM Attachment WHERE ParentId= '" + str(query_result[0].Id) + "'")
So far the results are good.到目前为止,结果很好。 Now I want to download the files uploaded to the case.
现在我想下载上传到案例的文件。 What should be my query?
我的查询应该是什么? I tried following and its always empty..But Im sure the case has files as well as attachments..
我试过跟随它,它总是空的..但我确定这个案例有文件和附件..
doc_result = service.query("SELECT Id, ContentDocumentId, Title FROM AttachedContentDocument WHERE Id= '" + str(query_result[0].Id) + "'")
I also tried the document object and still no success.我也尝试了文档对象,但仍然没有成功。 I appreciate your help.
我很感激你的帮助。
You can get not more than one Attachment by one request in any Salesforce API.在任何 Salesforce API 中,您最多只能通过一个请求获得一个附件。 To be sure that you don't an attachment, get the Attachment.Id first, then get the body by a loop
确保你没有附件,先获取Attachment.Id,然后通过循环获取正文
SELECT Id FROM Attachment WHERE ParentId = '...'
for ...
SELECT Body FROM Attachment WHERE Id = '...'"
A) SOAP API (Beatbox): Get the Attachment as a normal long base64encoded field. A) SOAP API (Beatbox):获取附件作为普通长 base64encoded 字段。
import base64
ret = service.query("SELECT Id, Body FROM Attachment WHERE Id = '...'")
blob = base64.b64decode(ret)[0]['Body'])
The query should expect one row because the output is restricted to one row if the "Body" field is present.查询应该期望一行,因为如果存在“正文”字段,则输出仅限于一行。
B) If REST API is used ( simple-salesforce package) with the same queries, the value of Body
or VersionData
fields is a URL of the form '/services/data/v40.0/sobjects/Attachment/<object_id>/Body'
which can be downloaded by GET request. B) 如果REST API 用于相同的查询( simple-salesforce包),则
Body
或VersionData
字段的值是形式为'/services/data/v40.0/sobjects/Attachment/<object_id>/Body'
可以通过 GET 请求下载。
C) Fetch Salesforce Attachment content with django-salesforce C)使用django-salesforce获取 Salesforce 附件内容
Objects useful for Binary Large OBjects are Attachment, Document and ContentVersion.对二进制大对象有用的对象是附件、文档和内容版本。 These useful queries allow to get a binary large object (Attachment or Document) as a normal long field by SOAP API (Beatbox).
这些有用的查询允许通过SOAP API (Beatbox) 将二进制大对象(附件或文档)作为普通的长字段获取。 The
ContentVersion
object allows to store more versions of the same data. ContentVersion
对象允许存储相同数据的更多版本。 Attachment
has a parent object. Attachment
有一个父对象。 Document
is without any parent object. Document
没有任何父对象。 Useful queries: (read restrictions by API above)有用的查询:(通过上面的 API 读取限制)
SELECT Id, Body FROM Attachment WHERE ParentId = '...'
SELECT Id, Body FROM Document WHERE ParentId = '...'
SELECT Id, VersionData FROM ContentVersion WHERE ...
att_body = att_result["records"][0]["Body"]
att_url = base_url + attachment_body
att_text_result = requests.get(url, headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer %s' % session_id })
Refer this for session_id: https://github.com/simple-salesforce/simple-salesforce请参阅此 session_id: https : //github.com/simple-salesforce/simple-salesforce
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.