simple-salesforce-pybaitz

A basic Salesforce.com REST API client.


Keywords
python, salesforce, com, api, api-client
License
Apache-2.0
Install
pip install simple-salesforce-pybaitz==0.74.2

Documentation

Simple Salesforce

image

Documentation Status

Simple Salesforce is a basic Salesforce.com REST API client built for Python 3.8, 3.9, 3.10, 3.11, and 3.12. The goal is to provide a very low-level interface to the REST Resource and APEX API, returning a dictionary of the API JSON response. You can find out more regarding the format of the results in the Official Salesforce.com REST API Documentation


Documentation

Official Simple Salesforce documentation

Examples

There are two ways to gain access to Salesforce

The first is to simply pass the domain of your Salesforce instance and an access token straight to Salesforce()

For example:

If you have the full URL of your instance (perhaps including the schema, as is included in the OAuth2 request process), you can pass that in instead using instance_url:

There are also four means of authentication, one that uses username, password and security token; one that uses IP filtering, username, password and organizationId, one that uses a private key to sign a JWT, and one for connected apps that uses username, password, consumer key, and consumer secret;

To login using the security token method, simply include the Salesforce method and pass in your Salesforce username, password and token (this is usually provided when you change your password):

To login using IP-whitelist Organization ID method, simply use your Salesforce username, password and organizationId:

To login using the JWT method, use your Salesforce username, consumer key from your app, and private key (How To):

To login using a connected app, simply include the Salesforce method and pass in your Salesforce username, password, consumer_key and consumer_secret (the consumer key and consumer secret are provided when you setup your connected app):

If you'd like to enter a sandbox, simply add domain='test' to your Salesforce() call.

For example:

Note that specifying if you want to use a domain is only necessary if you are using the built-in username/password/security token authentication and is used exclusively during the authentication step.

If you'd like to keep track where your API calls are coming from, simply add client_id='My App' to your Salesforce() call.

If you view the API calls in your Salesforce instance by Client Id it will be prefixed with simple-salesforce/, for example simple-salesforce/My App.

When instantiating a Salesforce object, it's also possible to include an instance of requests.Session. This is to allow for specialized session handling not otherwise exposed by simple_salesforce.

For example:

Record Management

To create a new 'Contact' in Salesforce:

This will return a dictionary such as {u'errors': [], u'id': u'003e0000003GuNXAA0', u'success': True}

To get a dictionary with all the information regarding that record, use:

To get a dictionary with all the information regarding that record, using a custom field that was defined as External ID:

To change that contact's last name from 'Smith' to 'Jones' and add a first name of 'John' use:

To delete the contact:

To retrieve a list of Contact records deleted over the past 10 days (datetimes are required to be in UTC):

To retrieve a list of Contact records updated over the past 10 days (datetimes are required to be in UTC):

Note that Update, Delete and Upsert actions return the associated Salesforce HTTP Status Code

Use the same format to create any record, including 'Account', 'Opportunity', and 'Lead'. Make sure to have all the required fields for any entry. The Salesforce API has all objects found under 'Reference -> Standard Objects' and the required fields can be found there.

Queries

It's also possible to write select queries in Salesforce Object Query Language (SOQL) and search queries in Salesforce Object Search Language (SOSL).

All SOQL queries are supported and parent/child relationships can be queried using the standard format (Parent__r.FieldName). SOQL queries are done via:

If, due to an especially large result, Salesforce adds a nextRecordsUrl to your query result, such as "nextRecordsUrl" : "/services/data/v26.0/query/01gD0000002HU6KIAW-2000", you can pull the additional results with either the ID or the full URL (if using the full URL, you must pass 'True' as your second argument)

As a convenience, to retrieve all of the results in a single local method call use

While query_all materializes the whole result into a Python list, query_all_iter returns an iterator, which allows you to lazily process each element separately

Values used in SOQL queries can be quoted and escaped using format_soql:

To skip quoting and escaping for one value while still using the format string, use :literal:

To escape a substring used in a LIKE expression while being able to use % around it, use :like:

SOSL queries are done via:

There is also 'Quick Search', which inserts your query inside the {} in the SOSL syntax. Be careful, there is no escaping!

Search and Quick Search return None if there are no records, otherwise they return a dictionary of search results.

More details about syntax is available on the Salesforce Query Language Documentation Developer Website

CRUD Metadata API Calls

You can use simple_salesforce to make CRUD (Create, Read, Update and Delete) API calls to the metadata API.

First, get the metadata API object:

To create a new metadata component in Salesforce, define the metadata component using the metadata types reference given in Salesforce's metadata API documentation

This custom object metadata can then be created in Salesforce using the createMetadata API call:

Similarly, any metadata type can be created in Salesforce using the syntax mdapi.MetadataType.create(). It is also possible to create more than one metadata component in Salesforce with a single createMetadata API call. This can be done by passing a list of metadata definitions to mdapi.MetadataType.create(). Up to 10 metadata components of the same metadata type can be created in a single API call (This limit is 200 in the case of CustomMetadata and CustomApplication).

readMetadata, updateMetadata, upsertMetadata, deleteMetadata, renameMetadata and describeValueType API calls can be performed with similar syntax to createMetadata:

The describe method returns a DescribeValueTypeResult object.

Just like with the createMetadata API call, multiple metadata components can be dealt with in a single API call for all CRUD operations by passing a list to their respective methods. In the case of readMetadata, if multiple components are read in a single API call, a list will be returned.

simple_salesforce validates the response received from Salesforce. Create, update, upsert, delete and rename methods return None, but raise an Exception with error message (from Salesforce) if Salesforce does not return success. So, error handling can be done by catching the python exception.

simple_salesforce also supports describeMetadata and listMetadata API calls as follows. describeMetadata uses the API version set for the Salesforce object and will return a DescribeMetadataResult object.

Up to 3 ListMetadataQuery objects can be submitted in one list_metadata API call by passing a list. The list_metadata method returns a list of FileProperties objects.

File Based Metadata API Calls

You can use simple_salesforce to make file-based calls to the Metadata API, to deploy a zip file to an org.

First, convert and zip the file with:

sfdx force:source:convert -r src/folder_name -d dx

Then navigate into the converted folder and zip it up:

zip -r -X package.zip *

Then you can use this to deploy that zipfile:

Both deploy and checkDeployStatus take keyword arguments. The single package argument is not currently available to be set for deployments. More details on the deploy options can be found at https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_deploy.htm

You can check on the progress of the deploy which returns a dictionary with status, state_detail, deployment_detail, unit_test_detail:

Example of a use-case:

Other Options

To insert or update (upsert) a record using an external ID, use:

To format an external ID that could contain non-URL-safe characters, use:

To retrieve basic metadata use:

To retrieve a description of the object, use:

To retrieve a description of the record layout of an object by its record layout unique id, use:

To retrieve a list of top level description of instance metadata, user:

Using Bulk

You can use this library to access Bulk API functions. The data element can be a list of records of any size and by default batch sizes are 10,000 records and run in parallel concurrency mode. To set the batch size for insert, upsert, delete, hard_delete, and update use the batch_size argument. To set the concurrency mode for the salesforce job the use_serial argument can be set to use_serial=True.

Create new records:

Update existing records:

Update existing records and update lookup fields from an external id field:

Upsert records:

Query records:

To retrieve large amounts of data, use

Query all records:

QueryAll will return records that have been deleted because of a merge or delete. QueryAll will also return information about archived Task and Event records.

To retrieve large amounts of data, use

Delete records (soft deletion):

Hard deletion:

Using Bulk 2.0

You can use this library to access Bulk 2.0 API functions.

Create new records:

"Custom_Id__c","AccountId","Email","FirstName","LastName"
"CustomID1","ID-13","contact1@example.com","Bob","x"
"CustomID2","ID-24","contact2@example.com","Alice","y"
...

Create new records concurrently:

Update existing records:

"Custom_Id__c","AccountId","Email","FirstName","LastName"
"CustomID1","ID-13","contact1@example.com","Bob","X"
"CustomID2","ID-24","contact2@example.com","Alice","Y"
...

Upsert records:

"Custom_Id__c","LastName"
"CustomID1","X"
"CustomID2","Y"
...

Query records:

Download records(low memory usage):

Delete records (soft deletion):

"Id"
"0000000000AAAAA"
"0000000000BBBBB"
...

Hard deletion:

Retrieve failed/successful/unprocessed records for ingest(insert,update...) job:

Using Apex

You can also use this library to call custom Apex methods:

This would call the endpoint https://<instance>.salesforce.com/services/apexrest/User/Activity with data= as the body content encoded with json.dumps

You can read more about Apex on the Force.com Apex Code Developer's Guide

Additional Features

There are a few helper classes that are used internally and available to you.

Included in them are SalesforceLogin, which takes in a username, password, security token, optional version and optional domain and returns a tuple of (session_id, sf_instance) where session_id is the session ID to use for authentication to Salesforce and sf_instance is the domain of the instance of Salesforce to use for the session.

For example, to use SalesforceLogin for a sandbox account you'd use:

Simply leave off the final domain if you do not wish to use a sandbox.

Also exposed is the SFType class, which is used internally by the __getattr__() method in the Salesforce() class and represents a specific SObject type. SFType requires object_name (i.e. Contact), session_id (an authentication ID), sf_instance (hostname of your Salesforce instance), and an optional sf_version

To add a Contact using the default version of the API you'd use:

To use a proxy server between your client and the SalesForce endpoint, use the proxies argument when creating SalesForce object. The proxy argument is the same as what requests uses, a map of scheme to proxy URL:

All results are returned as JSON converted OrderedDict to preserve order of keys from REST responses.

Helpful Datetime Resources

A list of helpful resources when working with datetime/dates from Salesforce

Convert SFDC Datetime to Datetime or Date object .. code-block:: python

import datetime # Formatting to SFDC datetime formatted_datetime = datetime.datetime.strptime(x, "%Y-%m-%dT%H:%M:%S.%f%z")

#Formatting to SFDC date formatted_date = datetime.strptime(x, "%Y-%m-%d")

Helpful Pandas Resources

A list of helpful resources when working with Pandas and simple-salesforce

Generate list for SFDC Query "IN" operations from a Pandas Dataframe

Generate Pandas Dataframe from SFDC API Query (ex.query,query_all)

Generate Pandas Dataframe from SFDC API Query (ex.query,query_all) and append related fields from query to data frame

Generate Pandas Dataframe from SFDC Bulk API Query (ex.bulk.Account.query)

YouTube Tutorial

Here is a helpful YouTube tutorial which shows how you can manage records in bulk using a jupyter notebook, simple-salesforce and pandas.

This can be a effective way to manage records, and perform simple operations like reassigning accounts, deleting test records, inserting new records, etc...

Authors & License

This package is released under an open source Apache 2.0 license. Simple-Salesforce was originally written by Nick Catalano but most newer features and bugfixes come from community contributors. Pull requests submitted to the GitHub Repo are highly encouraged!

Authentication mechanisms were adapted from Dave Wingate's RestForce and licensed under a MIT license

The latest build status can be found at Travis CI