简体   繁体   English

如何使用 Python 从 Salesforce 获取文件

[英]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包),则BodyVersionData字段的值是形式为'/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.

相关问题 如果 object 名称是动态的,如何使用 Python 获取 Salesforce 对象的元数据? - How to get metadata of Salesforce Objects using Python if the object name is dynamic? 如何使用python中的REST API获取salesforce帐户中的选项卡信息? - How to get the tabs information in a salesforce account using REST API in python? 如何使用Beatbox python API从Salesforce中提取原始数据 - How to extract raw data from Salesforce using Beatbox python API 使用Python从Salesforce URL下载图像 - Downloading images from Salesforce URL using Python 如何使用来自 OAuth autherization 的用户的 Salesforce 访问令牌通过 CData ODBC 驱动程序使用 python 访问 Salesforce 数据? - How to access Salesforce data through CData ODBC driver using python with using Salesforce Access Token of the user from OAuth autherization? 如何使用 python 在 Salesforce 中创建案例 - How to create a case in a Salesforce using python 如何从github安装salesforce python库 - How to install the salesforce python library from github 如何使用python从salesforce查询OpportunityFieldHistory - How to Query from salesforce the OpportunityFieldHistory with python 如何使用REST API获取Salesforce用户名? - How to get Salesforce username using REST api? 如何将从 salesforce 中提取的表格式化为 python? - How to format a table extracted from salesforce into python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM