LoginSignup
2
0

More than 1 year has passed since last update.

Pythonで動的にSoql作成

Last updated at Posted at 2021-09-01

・要件

Salesforceの項目変更される場合、Soqlに動的に反映する

・案 Pythonのsimple salesforceで実行する

Pythonのインストール(WindowsStoreで)

pip更新:python.exe -m pip install --upgrade pip
simple-salesforceの追加:pip install simple_salesforce

ログイン情報

security_tokenが‘’でも、下記用にSandbox(test)またProduct(login)でログインできます。
※DeveloperVersionができない?

sf = Salesforce(username='userid', password='password', security_token='', domain='test')
実装とCSV出力

※Csvに先頭属性という列は余計に出力されている

from simple_salesforce import Salesforce
import csv,unicodecsv


#(credentials hidden)

#sandbox or product OK

sf = Salesforce(username='Tableau@jp.ricoh.com.cirus.step2TBL', password='RjKanrinnnn', security_token='', domain='test')   
#developer version is not ok ???
#sf = Salesforce(username='chunqiang.hu_2@gmail.com', password='Hunnnnn', security_token='', domain='login')

desc = sf.Account.describe()  

# Below is what you need

field_names = [field['name'] for field in desc['fields']]

soql = "SELECT {} FROM Account".format(','.join(field_names))

print('sql:',soql)

results = sf.query_all(soql)
jsonD= results['records']

cnt = 0
with open('c:/tmp/test.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    for d in jsonD:
        if cnt == 0:  #csv header
            writer.writerow(d.keys())
            print(d.keys())
            print(d.values())
            cnt += 1

        writer.writerow(d.values())


print('result:OK')  
2
0
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
2
0