Scheduled jobs on Salesforce using Heroku and Beatbox Part 1

Here at Cloudjedi, we often have clients who require external data to be imported into salesforce at regular intervals. In most cases, the data is in another database, behind a REST API, or a file in an ftp server and thus, we needed to interface with them and make the necessary queries in order to obtain the desired data. There are times also that the job requires the script to ping hourly or every X minutes. In both of these cases, it's almost impossible to make use of vanilla apex. For this technical problem, we decided to make use of a python-based interface to Salesforce's SOAP API named beatbox to import our salesforce data for us and heroku as our platform to run the script on. For the first part of this series, I'll be talking about how to communicate with salesforce using beatbox:

Requirements:

  • Python 2.7.* and pip installed. You may want to use virtualenv for isolated environments also
  • Salesforce DevOrg Credentials - username, password and security token

Let's get started.

Initial Setup:

1. Setup your python environment and install beatbox by running in the command-line:

pip install beatbox

2. Let's create a folder for our project "bb2sf" and our main.py file where we will put our code

mkdir bb2sf

cd bb2f && touch main.py

3. Edit main.py and copy paste the code below:

import beatbox CONFIG = { 'SF_HOST': 'https://login.salesforce.com/services/Soap/u/27.0', 'SF_USER': <'USERNAME>', # Salesforce username 'SF_PASS': '<PASSWORD>', # Salesforce password 'SF_KEY' : '<SECURITY_TOKEN>', # Salesforce security token } sf = beatbox._tPartnerNS svc = beatbox.Client() svc.serverUrl = CONFIG['SF_HOST'] svc.login(CONFIG['SF_USER'], CONFIG['SF_PASS'] + CONFIG['SF_KEY'])

--what the this code simply does is login to your salesforce instance. Ideally, you should be storing the your salesforce credentials in an environment variable but we we'll store in a constant for the purposes of this post.

Inserting a Contact Object

1. Next, we will attempt to create a Contact object. Append this snippet to the code above:

obj = { 'type': 'Contact', 'FirstName': 'Johnabhelle', 'LastName': 'Maroney' 'Phone': '634-1892', 'Salutation': 'Prof' } result = svc.create(obj) if str(result[sf.success]) != 'true': print(result) else: print('Success!')

2. Run the script in the terminal using this command:

python main.py

If everything went well, you should see our newly added contact in your Salesforce DevOrg: --as you can see, inserting a salesforce object is as simple as creating a python object and running the beatbox 'create' method on it.

Updating a Contact Object

1. Next, let's try updating our inserted contact object. Grab the the Id of  our newly created contact and replace our create snippet with this one:

obj = { 'type': 'Contact', 'Id': '003900000102muQ', #Replace this with your contact Id 'FirstName': 'Jony', 'Title': 'His Majesty', 'Salutation': 'Dr' } result = svc.update(obj) if str(result[sf.success]) != 'true': print(result) else: print('Success!')

2. Again, run our script in the terminal:

python main.py

This should be the result:

That concludes this post for now. As you can see, it's quite easy to manipulate salesforce data using beatbox. It also has a lot of other useful methods such as Query, Upsert and Delete. You can check it's docs for more details.

In the next post, we will try to import data from a rest service into salesforce while the script runs on a schedule job. Stay tuned!