Using REST
In addition to built in functions that allow integrations with the Salesforce Apex and SOAP endpoints the Simple Salesforce package can be used to target the REST API. This example will demonstrate how to “Submit”, SFDC Submit documentation, a process approval in SFDC through the REST API.
Import any dependent packages to your script. For this example we use Simple Salesforce and the built in JSON package:
import json, simple_salesforce
Authenticate and create an instance of your Salesforce environment using SalesForceLogin:
sf = simple_salesforce.Salesforce(
username=sf_user,
password=sf_password,
security_token=sf_token)
Build the body of the request using information specific to your environment:
body = {"requests" : [{
"actionType": "Submit",
"contextId": "001D000000I8mIm",
"nextApproverIds": ["005D00000015rY9"],
"comments":"this is a test",
"contextActorId": "005D00000015rZy",
"processDefinitionNameOrId" : "PTO_Request_Process",
"skipEntryCriteria": "true"}]
}
Use the restful method to send a HTTP request to the REST API:
result = sf.restful('process/approvals/', method='POST', json=body)
A response will be returned in a JSON string that can be loaded into your script using the following:
resp = result
This response will match the following format:
[ {
"actorIds" : [ "005D00000015rY9IAI" ],
"entityId" : "001D000000I8mImIAJ",
"errors" : null,
"instanceId" : "04gD0000000Cvm5IAC",
"instanceStatus" : "Pending",
"newWorkitemIds" : [ "04iD0000000Cw6SIAS" ],
"success" : true } ]
You can learn more about SFDC REST and the available calls on Salesforce REST API Developer Guide