Salesforce.com integration

We will end this chapter with a quick overview of Salesforce integration. Salesforce is a cloud-based data management tool that allows organizations to store and manage their data in a collaborative way. Information about important contacts, related organizations, and any other custom piece of data that a team may need to share can be stored in Salesforce.

One advantage of using Salesforce to manage your data is that it includes many built-in reporting mechanisms. These are especially useful in medium-sized organizations that have enough people to make sharing data difficult, but do not have the time or resources to build a custom internal solution, like those we've seen earlier.

The difficulty with Salesforce, however, is that data must be entered. If you're collecting information via a Django application, it is stored in a local database. You could expose this data as we did in the beginning of this chapter using an API, but once exposed you still need tools to manipulate it.

Taking the Salesforce approach, we can push our Django database information into a Salesforce account, which can include custom objects that represent a subset of data we've collected in Django. These objects are defined via the Salesforce website, but typically resemble a table definition you would make in any SQL database.

Salesforce is accessible via a SOAP API and several Python wrapper libraries exist. One such library is salesforce-beatbox, which is available at: http://code.google.com/p/salesforce-beatbox/.

The beatbox application lets you receive and send information to your Salesforce instance. It requires very little in the way of Python code to get up and running. You will need to generate a security token from your Salesforce preferences page, but after you obtain this you can connect to the Salesforce API like so:

import beatbox
    
USER = '[email protected]'
PASSWD = 'xxxxx'
SECURITY_TOKEN = 'gds#mklz!'

svc = beatbox.PythonClient() tokenpass = '%s%s' % (PASSWD, SECURITY_TOKEN) svc.login(USER, tokenpass)

The svc object should now successfully be connected to Salesforce and we can begin querying our objects using the Salesforce query language, which they call SOQL:

result = svc.query("SELECT id,name FROM Merchandise__c")

The result variable contains a list of dictionary objects. Each dictionary has the keys specified in the SELECT statement and the corresponding values in the Salesforce table. Any custom object created in Salesforce will include a __c suffix when referenced in a query. We've created a Merchandise object in Salesforce, which we query above using the merchandise__c table.

Creating objects in Salesforce is equally as simple. To do so we need to construct a dictionary that includes the values for the object we want to create. A particularly important key that is required in all cases is type. The type key specifies what kind of Salesforce object we are creating. For example, the following dictionary sets up the new Merchandise object that we will create in Salesforce:

merch_data = {'name': 'Cranberry Sauce', 
                       'price_in_dollars__c': '1.50',
                       'type': 'Merchandise__c'}

Once we've created our data, we can send it to Salesforce using our svc object:

svc.create(data)

Updating Salesforce information is equally as straightforward. The only difference is that our data dictionary must include an id key whose value is the Salesforce unique ID number. This is in addition to a type key, which is also required for updates, as well as the fields which we want to update and their new values. It is not required that every field has a key in the update dictionary, only those we want to change. For example:

updated_data = {'id': '02111AC2DB987', 'type': 'Merchandise__c',
                'price_in_dollars__c': '1.75'} 
svc.update(updated_data)

Create and update calls to the Salesforce API can also perform bulk updates using their bulk API. This happens automatically when using beatbox when you construct your new or updated data as a list, instead of a single dictionary. Bulk updates are limited to 200 items per call, so some logic is required to slice your data lists to conform to this limit. It is also a requirement that all of the data you are sending to the Salesforce bulk API is of the same type.

Salesforce Object Query Language

When querying our Salesforce instance using the query API function above, we used a subset of SQL that Salesforce calls SOQL. SOQL is much simpler than most SQL implementations, but allows us to filter and slice our Salesforce data. Once we've issued a query to the Salesforce API, we can use Python to manipulate the returned data if we need further filtering.

SOQL exists exclusively for querying Salesforce. As a result, almost all SOQL commands resemble the SELECT statement in standard SQL implementations. But unlike most SQL systems, SOQL has limited the features of the SELECT statement. You cannot perform arbitrary joins, for example. Here is an example SOQL query:

SELECT Name, Description
FROM Merchandise__c
WHERE Name LIKE 'Cranberry'

In cases where we have relationships defined on our Salesforce objects, SOQL allows us to perform queries on these relationships using a special dot-syntax. Imagine the Merchandise object queried in the previous SOQL statement included a relationship to a Manufacturer object. To SELECT based on the related Manufacturer's field information would look like this:

SELECT Name, Description, Manufacturer.Address
FROM Merchandise__c
WHERE Manufacturer.Name == 'CranCo'

This SOQL query would return the name and description of all Merchandise in Salesforce that was manufactured by "CranCo". It would also include the manufacturer's address field in the resulting data. When using the Python beatbox module, these results are again returned as a list of dictionaries.

The Salesforce API is relatively small. We've seen two of the most important functions in these examples: query and create. There are additional functions for deleting, updating, and even defining new object types. The beatbox module supports almost all of the functions in the API, but not every one. For example, it does not currently support the undelete function. This compatibility information is documented in the beatbox README.txt.

With data in Salesforce, it suddenly becomes accessible to entire teams without any additional development work. This can be very useful in small organizations whose access to developers is limited. Non-technical staff can use Salesforce to run reports and pull information in an intuitive, web-based application. Often this data would be locked-up inside our web application, accessible just to those who can perform a Django ORM call or write a SQL query.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset