• image

Salesforce-Python: Parsing xml files from SF using Python

Integrating Salesforce-Python


Problem: Two weeks ago or maybe more our clients from California requested a script to parse a xml file (Salesforce-Python). After parsing the xml we had to get some data from it and update some Salesforce records using that data.

Salesforce-Python: Why Python?

  • Python is a short syntax language (fast coding). We didn’t have to worry about semicolons, type objects or braces.
  • Has a lot of useful and documented libraries. Easy integration with Salesforce.
  • Great support from the community

Salesforce-Python: Steps to set our environment before moving forward:

  • Install Python. It is the typical installation (Next, next, next etc…) . Python sets the environment variables for you. You don’t have to worry about anything. Install 2.7 because it has more support from the community. https://www.python.org/downloads/.
  • Install Anaconda. It allows you download and resolve the dependencies needed for your project. https://www.continuum.io/downloads.
  • Open your Anaconda console and install Pip (it allows you to manage the packages). Pip is generally installed by default in Anaconda but if it’s not, execute the next instructions: conda install pip.
  • Install Beatbox by Pip through the Anaconda console. Type this: pip install beatbox. Beatbox allows you to interact with Salesforce (query and make DML operations).
  • Install Netbeans to edit python (this is optional) or just use the Python GUI.

Salesforce-Python: How to install Beatbox

Salesforce-Python, Install Beatbox





Salesforce-Python, Python GUI




To resolve this problem we are going to work with 2 files: the Main class (where the script starts) and the XmlEngine class (this will contain methods to read xml and save the information in Salesforce)


Let’s see the main things to keep in mind:

These are the libraries needed to connect to Salesforce (Beatbox), parse the xml (xml.etree.ElementTree) and throw exceptions (traceback).

Connecting to Salesforce. It takes the data from a JSON file, you can put it directly. Append the token to the password.


This block of code parse the xml file:

Beatbox require an especial format (JSON) to make an update, also don’t forget to indicate the record id and the object type otherwise Beatbox can’t interpret where is the update needed.

Get the records that you need get updated. Query to Salesforce.


This class is the start point of the script. Imports the XmlEngine module/class in order to execute the script.

Salesforce-Python: Execute the script

Open the Anaconda console and go to the src directory of the project (where the Main.py file is).  Run the script typing: python Main.py

Salesforce-Python, Run Script






Leave a Reply

Your email address will not be published. Required fields are marked *