LoginSignup
6
6

More than 3 years have passed since last update.

PythonでSalesforce REST APIとやりとりするときのサンプル

Last updated at Posted at 2020-04-15

注:テスト投稿のため、細かな解説等はありません。

simple-salesforce

README を読めばおおよその利用方法が書いてありますが、
より詳しく使うには、ソースコードを追っていく必要があります。
https://github.com/simple-salesforce/simple-salesforce

認証

あくまで一例なので、組織に応じた認証方式でログインをしてください。
https://github.com/simple-salesforce/simple-salesforce/blob/master/simple_salesforce/login.py#L24


from simple_salesforce import Salesforce

def instance():
    return Salesforce(username=SALESFORCE_USERNAME, password=SALESFORCE_PASSWORD, security_token=SALESFORCE_SECURITY_TOKEN, sandbox=SALESFORCE_IS_SANDBOX)

if __name__ == "__main__":
    sf = instance()

Pandasで適宜データを加工してREST APIをコールする

取引先オブジェクトがPandasのDataFrameとして作成済みで、
ヘッダー行がSalesforceのカラム名で定義済みであるならば、以下のように


import pandas as pd

data = dataframe.to_dict(orient='records')
sf.bulk.Account.insert(data)

bulk API経由でコールしたいときは上のように書いてあげると呼び出せます。

PandasのDataFrameをそのまま一気にSalesforceへ投げ込める手軽さがメリットで、
CSV、JSON、別DB、その他ファイルにあるデータを一旦DataFrameで加工してあげるだけで、
データ移行や連携がすぐに組めます。

6
6
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
6
6