To get started in the simples of ways, you would do the following
from salesforce_api import Salesforce
client = Salesforce(username='test@example.com',
password='my-password',
security_token='password-token')
If you are trying to connect to a sandbox, you have to specify this using the is_sandbox argument.
from salesforce_api import Salesforce
client = Salesforce(username='test@example.com',
password='my-password',
security_token='password-token',
is_sandbox=True)
If for some reason the login-url differs from the standard prod/test login urls, you can specify the login url. This can be useful if you are using a mock-server, for example. This will override the argument.
is_sandbox
from salesforce_api import Salesforce
client = Salesforce(username='test@example.com',
password='my-password',
security_token='password-token',
domain='login.example.com')
The examples so far would use the SOAP API for authenticating. If you want to authenticate using an app, that's easy engough. The login-url and sandbox-arguments applies here as well.
from salesforce_api import Salesforce
client = Salesforce(username='test@example.com',
password='my-password',
client_id='123',
client_secret='my-secret')
You can also authenticate using the OAuth Client Credentials Flow given that's enabled for your connected app.
from salesforce_api import Salesforce
client = Salesforce(client_id='123',
client_secret='my-secret',
domain='my-domain')
If you already have an OAuth access token, obtained elsewhere, you can just as easily create a new client.
from salesforce_api import Salesforce
client = Salesforce(access_token='access-token-here',
domain='access-token-domain')
If you want to explicitly use one or the other methods of authenticating, you can do that as well
The Salesforce API is great at returning large amounts of data, so the pagination that Salesforce implements for the result of your queries is taken cared of automagically when querying for data.
Example
client.sobjects.query("SELECT Id, FirstName, LastName FROM Contact WHERE FirstName='Felix'")
[<FailResultRecord record_id="0031l000007rU5rAAE" success="False" error="ENTITY_IS_DELETED:entity is deleted:--" />,
<FailResultRecord record_id="0031l000007rU5sAAE" success="False" error="ENTITY_IS_DELETED:entity is deleted:--" />]
Manual managing bulk job
By using the api above, the library hides the uploading and waiting for the bulk-process to get processed.
In some cases you might want to handle this differently. Perhaps you want to upload bunch of records to be inserted and then forget about the process. This can be done by creating a job and managing it by yourself.
bulk_job = client.bulk.create_job(OPERATION.INSERT, 'Contact')
bulk_job.upload([
{'LastName': 'Lindstrom', 'Email': 'test@example.com'},
{'LastName': 'Something else', 'Email': 'test@example.com'}
])
while not bulk_job.is_done():
time.sleep(5)
from salesforce_api.models.deploy import Options
deployment = client.deploy.deploy('/path/to/file.zip')
deployment.wait()
result = deployment.get_status()
Only validating
from salesforce_api.models.deploy import Options
deployment = client.deploy.deploy('/path/to/file.zip', Options(checkOnly=True))
deployment.wait()
result = deployment.get_status()
Validating running specific tests
from salesforce_api.models.deploy import Options
deployment = client.deploy.deploy('/path/to/file.zip', Options(
checkOnly=True,
testLevel='RunSpecifiedTests',
runTests=[
'TesterIntegrationApplicationTest',
'TesterIntegrationProcessTest'
]
))
deployment.wait()
result = deployment.get_status()
Canceling a deployment as soon as it fails
from salesforce_api.models.deploy import Options
deployment = client.deploy.deploy('/path/to/file.zip', Options(checkOnly=True))
while not deployment.is_done():
if deployment.has_failed():
deployment.cancel()
break
Retrieving
Example
from salesforce_api.models.shared import Type
# Decide what you want to retrieve
types = [
Type('ApexTrigger'),
Type('ApexClass', ['MyMainClass', 'AnotherClass'])
]
# Create retrievement-job
retrievement = client.retrieve.retrieve(types)
# Wait for the job to finish
retrievement.wait()
# Write the resulting zip-archive to a file
open('retrieve.zip', 'wb').write(retrievement.get_zip_file().read())
Additional features
If for some reason you need to specify how the client communicates with Salesforce, you can create the requests-session yourself and pass it to the client upon creation. This would, for example, allow you proxy your requests: