SalesforceXyToolsCore/Python上でSalesforceのREST APIへアクセス

Topic

Apexclass内容を取得する

  • Salesforce組織のユーザ名、パスワード、Apiバージョン、Product/Sandboxを設定してください。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from SalesforceXytoolsCore import *
import pprint
config = {
"api_version": 42.0,
"username": "sfdc username",
"password": "sfdc password",
"security_token": "",
"is_sandbox": True
}
rest_api = RestApi(username=config["username"],
password=config["password"],
security_token=config["security_token"],
sandbox=config["is_sandbox"],
version=config["api_version"]
)
"""
Run Rest API : query apexclass
"""
sel_string = "Select Id, Name From ApexClass Limit 3"
params = {'q': sel_string}
result = rest_api.restful('tooling/query', params)
pprint.pprint(result)

結果を確認

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
OrderedDict([('size', 3),
('totalSize', 3),
('done', True),
('queryLocator', None),
('entityTypeName', 'ApexClass'),
('records',
[OrderedDict([('attributes',
OrderedDict([('type', 'ApexClass'),
('url',
'/services/data/v42.0/tooling/sobjects/ApexClass/01pXXXXXXXXXXXXX')])),
('Id', '01pXXXXXXXXXXXXX'),
('Name', 'OpportXXXXXXX')]),
OrderedDict([('attributes',
OrderedDict([('type', 'ApexClass'),
('url',
'/services/data/v42.0/tooling/sobjects/ApexClass/01pXXXXXXXXXXXXX')])),
('Id', '01pXXXXXXXXXXXXX'),
('Name', 'DaoXXXXXXXXXXXXX')]),
OrderedDict([('attributes',
OrderedDict([('type', 'ApexClass'),
('url',
'/services/data/v42.0/tooling/sobjects/ApexClass/01pXXXXXXXXXXXXX')])),
('Id', '01pXXXXXXXXXXXXX'),
('Name', 'ExiaXXXXXXXXXXXXX')])])])

ApexCodeCoverageカバー率を取得する

1
2
3
4
5
6
7
8
9
10
11
12
"""
Run Rest API : query ApexCodeCoverage
"""
sel_string = "SELECT Id, ApexTestClassId, TestMethodName, ApexClassorTriggerId, NumLinesCovered, NumLinesUncovered, Coverage FROM ApexCodeCoverage"
params = {'q': sel_string}
result = rest_api.restful(
path='tooling/query',
params=params,
method='GET'
)
pprint.pprint(result)

Sobjects一覧を取得する

1
2
3
4
5
6
7
8
9
10
"""
Run Rest API : search sobjects
"""
result = rest_api.call_rest(
method='GET',
path='/services/data/v37.0/sobjects',
params={},
)
pprint.pprint(result)

結果を確認

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{'encoding': 'UTF-8',
'maxBatchSize': 200,
'sobjects': [
{'activateable': False,
'createable': True,
'custom': False,
'customSetting': False,
'deletable': True,
'deprecatedAndHidden': False,
'feedEnabled': True,
'keyPrefix': '006',
'label': 'Opportunity',
'labelPlural': 'Opportunities',
'layoutable': True,
'mergeable': False,
'mruEnabled': True,
'name': 'Opportunity',
'queryable': True,
'replicateable': True,
'retrieveable': True,
'searchable': True,
'triggerable': True,
'undeletable': True,
'updateable': True,
'urls': {'approvalLayouts': '/services/data/v37.0/sobjects/Opportunity/describe/approvalLayouts',
'compactLayouts': '/services/data/v37.0/sobjects/Opportunity/describe/compactLayouts',
'defaultValues': '/services/data/v37.0/sobjects/Opportunity/defaultValues?recordTypeId&fields',
'describe': '/services/data/v37.0/sobjects/Opportunity/describe',
'layouts': '/services/data/v37.0/sobjects/Opportunity/describe/layouts',
'listviews': '/services/data/v37.0/sobjects/Opportunity/listviews',
'quickActions': '/services/data/v37.0/sobjects/Opportunity/quickActions',
'rowTemplate': '/services/data/v37.0/sobjects/Opportunity/{ID}',
'sobject': '/services/data/v37.0/sobjects/Opportunity'}},
................
................
.....省略.......
................
................
]
}