LoginSignup
0
2

More than 5 years have passed since last update.

SalesforceXyToolsCore/Python上でSalesforce組織の添付ファイルを一括ダウンロードする

Posted at

The original page link

SalesforceXyToolsCore/Python上でSalesforce組織の添付ファイルを一括ダウンロードする

Topic

  • SalesforceXyToolsCoreを使ってSalesforce組織の添付ファイルを一括ダウンロードする

Attachment

User が親オブジェクトにアップロードおよび添付したファイルをダウンロードする。
Bodyをファイルに保存すれば、完了です。

Salesforce組織の添付ファイルを一括ダウンロードする

  • Salesforce組織のユーザ名、パスワード、Apiバージョン、Product/Sandboxを設定してください。
from SalesforceXytoolsCore import *
import pprint

config = {
    "api_version": 42.0, 
    "username": "sfdc username", 
    "password": "sfdc password", 
    "security_token": "", 
    "is_sandbox": True
}

SAVE_DIR = './attachments_download'
if not os.path.exists(SAVE_DIR):
    os.mkdir(SAVE_DIR)

rest_api = RestApi(username=config["username"], 
                password=config["password"], 
                security_token=config["security_token"], 
                sandbox=config["is_sandbox"],
                version=config["api_version"]
                )

attachments = rest_api.query("SELECT Id, Name, Body FROM Attachment LIMIT 2000")
print("添付ファイル件数 : " + str(len(attachments)))

for attachment in attachments["records"]:
    print("start to download : " + attachment["Name"])
    """
    Run Rest API : download attachment
    """
    rest_path = "/services/data/v42.0/sobjects/Attachment/{id}/Body".format(id=attachment["Id"])
    result = rest_api.call_rest(
        method='GET',
        path=rest_path, 
        params={},
    )
    with open(os.path.join(SAVE_DIR, attachment["Id"] + "_" + attachment["Name"]), mode='wb') as f:
        f.write(result.content)

実行すれば、添付ファイル件数ファイルをダウンロードする可能です。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2