Creating csv extract from Salesforce (using Python) — Quick and Dirty Learning

Virendra Pratap Singh
2 min readFeb 11, 2021

Very frequently I resort to quick and dirty learning short-cuts. One reason I avoid, whenever possible, reinventing the wheel and second, more importantly, these short-cuts provide me with a starting point for diving deep. Here is a small Python solution that I prepared for quick practice — creation of csv extract of Salesforce data.

Like any Python solution, it uses a wonderful library already available (simple_salesforce). The idea is to create a SOQL that is passed to a session created by this library and fetch the data in a data frame.

So below is the simple SOQL and its resulting set of records -

Here goes the super-short code—

And here is the generated csv file–

Most of the code is pretty simple and self-explanatory. The items worth pointing are –

1) The SOQL used can be pretty complex (I have just used a where clause and limit to keep example simple).

2) Salesforce attributes are returned as well — we don’t really need them as part of our extract, and we can drop them from our data frame.

3) Headers are included by default — I put them to show that they may be turned off by setting it to False.

4) There are multiple options to log to Salesforce (including using private key) but are not shown here.

5) Since data is captured as a data frame, it makes it pretty easy to play with it (in case you want to use it for something beyond writing a csv file).

There are options of inserting and updating records in Salesforce in same Python library, but they are for another quick and dirty writeup later.

--

--

Virendra Pratap Singh

Snowflake, AWS, Python, MuleSoft and literally anything to do with data manipulation and analysis!! Love simple solutions and willing to learn from anyone.