Category Archives: salesforce

Using Salesforce API with python to create Cases and send Emails

This is an area which is pretty hard to find information about. A lot of people simply use Salesforce for accounts and sales, which means that they do not use Servicedesk at all.

When I wanted to integrate Salesforce from a SIEM solution I thought, how hard can it be. First of all, it was not hard when I finally found out how to do it. But getting there took a few hours. Here is how you can do it.

There is a nice python toolkit that is pretty outdated, but it works like a charm

https://code.google.com/p/salesforce-python-toolkit/

Start by donwloading it.
The next step is gather a few things from your Salesforce installation. You need these:

* Your security token for the account that you are going to use.
(Help here: https://help.salesforce.com/apex/HTViewHelpDoc?id=user_security_token.htm)

* Your username and password for the account that you are going to use.

* The WSDL of your installation. In my case it was the Enterprise WSDL.
(Help here: https://www.salesforce.com/developer/docs/api/Content/sforce_api_quickstart_steps_generate_wsdl.htm)

Once you have all these things follow these steps. I did this on Debian, but it will work the same on any Linux dist.

1. Install salesforce python toolkit (python setup.py install)

2. Save the WSDL as a file called wsdl.xml on your local system. In my example below I have created a folder in /root/ called salesforce and put it there.

3. Create the file below, called main.py and place it in the same folder.

4. Now you should have two files, one file called “main.py” and one called “wsdl.xml” in the same folder.

5. The next step is to replace some of the items in the code above. Start with changing the receiver e-mail. This is just an example on how you send e-mail via Salesforce.

6. Add your credentials (user,password,token) to the row at the top where you do the login.

7. The final thing is the AccountId. This is the Company (or Account in salesforce terms) that will be the owner of the Case when it is created. You can put your own Company id here for example. AccountId is located when you browse an account and look at the URL. It will be something like “001g00000XLyIxiF”. It starts with 001. Take that string and put it at the AccountId.

8. Done!

If you want to do more, have a look in the WSDL file. It is pretty straight forward and you can do a lot of things. If you search for “SingleEmailMessage” or “Case” you can find the objects and you see what kind of parameters you can set. Cases are of type Object so when you create them you use the create() function. As seen above. Read more here:

https://www.salesforce.com/developer/docs/api/
https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_case.htm
https://varunver.wordpress.com/2014/02/24/installing-salesforce-python-toolkit/
Good luck!