Ruby on Rails and Salesforce

How you can integrate Ruby on Rails and Salesforce? This question and more will be resolved in this article. Let’s start! Ruby on Rails (Rails) is an open-source web application framework, written in the Ruby programming language, that is used in many big-name applications, like GitHub, Airbnb, Hulu, Zendesk, and more. And as with all open-source projects, you can always contribute code. 

The Rails Doctrine

Rails has some interesting pillars that every developer should know: 

  1. Optimized for programmer happiness – “Ruby is designed to make programming not only easy but also fun” (Yukihiro Matsumoto)
  2. Convention over Configuration – The environment in which you work assumes many logical situations by default, so if you adapt to them rather than creating your own rules each time, programming becomes an easier and more productive task
  3. Rails is ‘omakase’ – Omakase is when you don’t know what to order in a restaurant and let the chef choose. In Ruby, this is the practice of letting others assemble your stack for you.

Check out the official site of Ruby on Rails and go deep into Ruby.

Integrating Ruby on Rails (Rails) with Salesforce

Salesforce, as the leading CRM, is constantly working to innovate, making it a great platform to integrate your applications with. Rails, like many other platforms, is frontend agnostic, giving you the flexibility to choose the tools and technologies that make the most sense for your project. Salesforce brings a robust set of APIs into the mix, letting you make use of the data and logic in Salesforce within your Ruby application. 

Pick the Right API(s)

This is the one area where you’ll need to make some choices, depending on what you need your application to do with Salesforce. Knowing which APIs you need will help guide which gems to install.

  • SOAP API – Create, retrieve, update or delete records, such as accounts, leads, and custom objects. Allows you to maintain passwords, perform searches, and much more. Optimized for real-time client applications that update a few records at a time. Indeed you can use it for processing many records, but when the data sets contain hundreds of thousands of records, SOAP API is less practical. Process data from a few thousand to millions of records very simple with the Bulk API.
  • REST API – Powerful, convenient, and simple REST-based web services interface for interacting with Salesforce. Based on the same underlying data model and standards objects as the SOAP API. (Other REST APIs that may be of interest: User Interface API, Connect REST API.)
  • Bulk API – This is based on REST principles and is optimized for loading or deleting large sets of data. You can use it to query, queryAll, insert, update, upsert, or delete many records asynchronously by submitting batches. Salesforce processes batches in the background. Also, another benefit of the Bulk API is that it has higher record limits.

Get Authenticated

Salesforce leverages OAuth 2.0 for authentication. There’s no need to code any of this from scratch, just leverage the Ruby gems (ruby software packages) restforce and omniauth-salesforce. Check out connectSFDC for a rails project with omniauth-salesforce installed and ready to interact with a Salesforce Connected app.

Integrate Your Application

If your application needs real-time access and is only updating a few records at a time, you can make use of the Restforce gem to access the REST API.

  1. Add ‘restforce’ or install manually with ‘gem install restforce’)
  2. Instantiate your client object and authenticate using OAuth.
    ‘Client =’
  3. Query away. (See the official Restforce documentation for all your options.)

For the SOAP API, use Soapforce

  1. Add ‘soapforce’ to your Gemfile or install manually with ‘gem install soapforce’.
  2. Instantiate your client object and authenticate using OAuth.
    ‘client =’
  3. Query as needed. (See the Soapforce Github page for options.)
  4. Log out.

For the Bulk API, use SalesforceBulkAPI

  1. Add ‘salesforce_bulk_api’ to your Gemfile or install manually with ‘gem install salesforce_bulk_api’
  2. Authenticate using restforce with OAuth.
    ‘client =…’
  3. Instantiate using the Restforce client.
    ‘salesforce =’
  4. Query as needed. (See the SalesforceBulkAPI page for options.)

Much of the groundwork has been done for you with these gems. The robust Salesforce APIs make it easier to bring the functionality and data into your Rails app. (Not into Ruby? You could always check out our article on Python with Salesforce.) 

Let us know how you’re approaching your Ruby on Rails and Salesforce projects.

Footer - Ruby on Rails and Salesforce

Salesforce Classic vs Lightning: More than a pretty interface

When we first published “Lightning Experience vs. Salesforce Classic: Is the Lightning Experience Revolutionizing Salesforce?”. Over two years ago there were many features missing from the Salesforce Lightning Experience. Making the conversation about the UI and what was missing. Certainly, over the last two years, Salesforce has significantly closed those gaps. Now, only discussing the Lightning UI misses many of the benefits it has introduced. 

User Experience

Often the most controversial part of any redesign, the user experience in Lightning is significantly different than Classic. It’s common for users of a product to resist changes to their experience. Lightning brings benefits for those that do. Whether your users are selling, helping, collaborating, or marketing, there are improvements to help them get their work done faster. Salesforce worked to simplify the user experience and create more efficient navigation. That allows you to not only change records quickly but change applications.

Significant work has gone into refreshing record layouts, dashboards, and report views. When Lightning was first introduced, admins and users were finding themselves having to switch back to Classic for specific tasks. Now that happens far less often. With the Lightning App Builder, it’s easy for admins to create new custom page layouts. The benefits aren’t limited to administrators. Here are a few areas the experience has improved.

 Salesforce Lightning Experience

  • Home 

Salesforce has transformed the way you start your day by adding an intelligent Home page. Sales reps can monitor their performance to goal and get insights on their key accounts, with an assistant providing the complete list of things to do. Admins can use the Lightning App Builder to create custom Home pages.

  • Opportunity Workspace

The new design allows you to help your sales reps work their deals faster. Lightning enables you to showcase key record details in the new highlights panel at the top of the page. Also, get key coaching details with a customizable Path to support your sales process. And, see a wealth of related information on hover using a quick view.

  • Accounts and Contacts

Lightning has optimized the layout for accounts and contacts, organizing the content by their primary use case: reference. Your sales team can locate important data efficiently, get the latest news for your customers, work smarter, keeping the data clean with field-level duplicate matching, among other functions.

  • Reports and Dashboards

Now users can create their filters while viewing a report. Also, if you already have reports from Salesforce Classic, the transition is very easy, because the reports are automatically viewable in the new interface.


Meanwhile, when someone says Lightning, it’s common for people to think just about the new user experience. Wrong, Lightning brings even more. Lightning as a platform has improved the experience for users, administrators, and developers. With new frameworks, Salesforce has made it significantly easier to create new pages and applications and made data access easier to manage. 

The new Lightning architecture takes advantage of modern web stacks that are now more standards-based, with Lightning Web Components providing a layer of specialized Salesforce services. You no longer need a proprietary component model, proprietary language extensions, proprietary modules, etc. For example, there are many significant benefits such as:

  • Common component model
  • Common programming model
  • Transferable skills and easier-to-find / easier-to-ramp-up developers
  • Interoperable components
  • Better performance because core features are implemented natively in web engines instead of in JavaScript in framework abstractions


Developing in Lightning requires a mind shift, but opens doors. Salesforce Classic was built around a page-centric model, while Lightning is based on an app-centric one. Basically, now that Classic Visualforce is only one (older) option, it’s possible to build web applications that are mobile-ready and run natively in browsers, even faster. Furthermore, the new lightning component architecture gives you two programming models: the Lightning Web Components model and the original Aura Components model. 

in addition, if you are currently developing Lightning components with the Aura programming model, you can continue to do so. Both can coexist and interoperate. But, in the future, we recommend you consider migrating your Aura Components to Lightning Web Components. Start with the components that would benefit the most from the performance benefits of Lightning Web Components.

“Lightning offers us the opportunity to use Lightning Web Components, these allow us to perform faster tasks with custom components, which can be used in different pages with just a drag and drop. It has great compatibility with different browsers which allows us to work without problems on desktop and mobile devices.”

Alexis M, Developer

“With Lightning Web Components I have been capable to develop reactivity, responsive and scalable solutions, thanks to its state-of-the-art web architecture that integrates features like Lightning Design System and MCCV model, leading to smoother development and a more comfortable user experience.”

Christian R, Developer

Check out one of our latest workshops. Mateo H, developer, shares one of the latest components our team has been working on:

If you want to discover more about Salesforce Lightning, we recommend you check out this module on Trailhead. That is to say, if you want to go deep into the main differences check out the Salesforce help page: Compare Lightning Experience and Salesforce Classic

Our team has also worked with different organizations and their projects. We are Salesforce platform experts and we offer custom development to help you build your platform and solve the right problems. If you want to know more about our work, go check out our latest success stories.

How to set a tamper-proof Salesforce session cookie with Apex

Imagine we have a website in which we want the user session to persist for a specific period, even when the user closes and reopens the browser. 

Our goal is to have operations/logic for a particular user persist on the website. Whenever the user returns to the site within a given amount of time using a session cookie.

There are a multitude of ways to store the session data. In this article, we are focusing on client-side storage (cookies). Cookies are small pieces of data that are transmitted from the server to the client (generally done once.) Then when the user comes back to the site, the cookies are sent back to the server. This allows us to track a single user across multiple connections to our site. 

Why should I care about the expiration?

Being able to easily expire user sessions allows for extra security measures. In our case, if we wanted to add a new feature that allows us to sign out of all sessions in other locations (machines where the user has logged in), this (expire session cookie) would force those other locations to re-authenticate before gaining access to the account.

This is a good security approach for when a user’s cookie is stolen or his credentials are compromised. Upon changing his password all his sessions are invalidated. An attack using an old cookie cannot continue to wreak havoc on the user’s account.

Signing your session cookie

As we know, all data stored on the client-side could potentially be compromised as a user can maliciously tamper with it. And since we are not able to avoid it, we might provide the server with the feature to recognize this manipulation. 

This feature consists of a cryptographically signed cookie. Upon receiving the cookie from the client, verify that the signature matches what you are expecting. 

HMAC (Hash-based message authentication code) is a cryptographic construct that uses a hashing algorithm (SHA-1, SHA-256, SHA-3) to create a MAC (message authentication code) with a secret key. Salesforce provides us with a class named Crypto that contains methods for creating digests, message authentication codes, and signatures, as well as encrypting and decrypting information. Click here to see more about Crypto.

Let’s code!

We are going to create a global helper class for signing session cookies. That we will be able to re-use in any part of our project.


Within this class, we are using other Salesforce helper classes such as Blob (Contains methods for the Blob primitive data type) and EncodingUtil (to encode and decode URL strings, and convert strings to hexadecimal format).

This allows us to do the following:


We can then send that to the client that requested the page. Once the client visits the next page, their browser will send that same cookie back to use. 

Assuming we need to store the ‘session ID’, we would get this as result:



All cookies created from Salesforce contain ‘apex__’ as a prefix and are encoded before being set for the page.  Learn more about cookies here

To retrieve and verify the data was not tampered with, we could do the following:


If the MAC is verified, it means that the data was not tampered with by the user and we can continue our business logic. 

Real Secret Key

For extra security measures, we might set an auxiliary CustomSetting for storing AES secret keys. We will create a Secret__c CustomSetting with a field named Base64HmacKey__c.

Now, we are going to refactor our code to include this new approach:

  • Add a new variable to our helper class
  • Add a new function to get the secret key from the CustomSetting
  • Lastly, modify the private variable named SECRET_KEY

Now we have better handling of our secret key.


When using client-side storage, it may be beneficial to encrypt the data to add an extra layer of security. Even when encrypting the data, you need to continue using a MAC.

Using just encryption will not protect you against decrypting bad data because an attacker decided to provide invalid data. Signing the cookie data with a MAC makes sure that the attacker is not able to mess with the ciphertext.

If you’ve made it this far, thanks for reading! Also, if you are interested in Salesforce development go and check our latest articles here.

Salesforce Certification: Platform Developer II

Congratulations on passing the Salesforce Platform Developer I certification exam and deciding to put your knowledge to the test, even further, by taking the Salesforce Platform Developer II certification exam. It may seem daunting at first, but with some studying and determination, you’ll get that certification under your belt in no time! Coming from someone who has only been familiar with the Salesforce platform for a few months, believe me when I say it is hard but certainly achievable. 

My Background

I’ve been working for Oktana as a Staff Software Engineer a few months and, prior to joining, had never heard of the Salesforce platform. Since then, I’ve completed numerous trails on Trailhead, worked on a few projects and passed both developer exams. I’ve learned a lot since I started working with Salesforce. By studying for the exams as well as working on the projects I was assigned, I feel as though I’ve learned more than I would have by just doing projects. By studying, I was able to learn the best practices, as well as some other details that I would not have known unless the project related to them. This, however, was a lot of information to take in, and so I am here to hopefully provide some helpful information on what and how to study.

General Recommendations

As with any certification exam, there are a number of recommendations that could help to get a better score. Things like:

  • Schedule the test at a time that is convenient for you. If you’re not a morning person, don’t schedule it in the morning. Do it at a time that you will be most awake and focused.
  • Make sure you know the time that you have to take it. Set a reminder on your phone, your calendar, anything to help you remember. I had to learn the hard way on my second attempt, as I didn’t pay attention to the fact that it was on a 12-hour clock, and I thought it was at 12:15 PM when it was really at 12:15 AM, 12 hours earlier (yes, they schedule that late). Luckily I was able to retake it the following day.
  • Eat a healthy meal that promotes focus and doesn’t make you feel sluggish.
  • Take your time and don’t rush. You have two hours, and you should use every second to your advantage.

Salesforce Developer II Exam Overview

The exam is broken down into five sections. They are currently weighted as follows:

  • Advanced Developer Fundamentals – 18%
  • Process Automation, Logic, and Integration – 24%
  • User Interface – 20%
  • Testing, Debugging, and Deployment – 20%
  • Performance – 18%

When compared to the first test, the second test is much harder and covers a wider range of topics. This is to be expected as it is a higher certification, but should not scare you. To prepare for it, here are some tips about this specific test.

Focus on Force

The site, Focus on Force, has plenty of great practice tests you can go through that are pretty similar to the actual questions. In fact, they also do a good job of breaking out and describing each section of the test which you can see here: Salesforce Platform Developer II Certification Contents

As you go through each section, I highly recommend carefully examining and learning why you got a question wrong, and what the correct answer is. Keep doing so until you take all the practice tests, and do them over and over until you get perfect scores. One test in particular that helped me was the question bank test. It is a collection of 20 questions picked at random from each of the sections. The low question count allows you to quickly complete a test, see the results, and then retake it. If you are on the go, the mobile view of the website is good as well and can enable you to study anywhere. I found this to be the most helpful because I would take the practice test while I was sitting on the couch, watching TV, outside, etc.

Apart from the practice tests on Focus on Force, they have study guides that are extensive and very helpful. If you read through them all and take notes, you’ll end up with an abundance of information to study. Using these notes, I was able to make flashcards that I used to study.


Flashcards are a great way to study. In my case, it was mainly beneficial to write down and make the flashcards, as opposed to just looking and memorizing things. When you type or write out flashcards, I find it helps commit information to memory. I was able to make flashcards out of the notes I took on the Focus on Force study guides and used Quizlet to flip through them while I was out and about. This helps especially with memorizing specific information pertaining to numbers, percentages or, everyone’s favorite, governor limits. When faced with those types of questions, there isn’t a way to work through and narrow down the answer as much as the others. It’s either you know the answer or you don’t, and this helps to make sure you are able to answer confidently.




In order to receive your certification, you must complete the required super badges. They are currently:

  • Apex Specialist: Use integration and business logic to push your Apex coding skills to the limit
  • Data Integration Specialist: Demonstrate your integration skills by synchronizing external data systems and Salesforce
  • Advanced Apex Specialist: Build complex business logic using advanced Apex and Visualforce programming techniques

These superbadges can be completed before or after the exam, but I strongly suggest that you complete them beforehand. Of course, if you’ve passed the Salesforce Platform Developer I exam and received your certification, you’ll already know how beneficial they can be. But, it doesn’t hurt to stress the importance of these superbadges. They force you to look up information in the documentation and then implement that in the exercise. If you do these superbadges beforehand, you gain experience and hands-on knowledge before you take the test so that you can get a better, more informed, perspective on the question.

When You’re Ready For the Exam

The most important thing to remember about taking the test is to read each question and answer very carefully. The questions and answers are extremely tricky. The questions usually come down to two possible answers. From there, you must go through both choices to examine the small details or differences between the two. Don’t be afraid to mark the questions for later if you don’t know the answer right away. Once you get the questions that you know for sure out of the way, you can use the available time to go through the skipped questions to see if you know it now or not. If you don’t pass, remember those questions you were stuck on and research the subject pertaining to them so you can see why you didn’t get it. Once you know this, the second attempt will go by a little smoother.

If you don’t pass, don’t get discouraged! Just schedule the retake as soon as possible so you don’t forget anything, and use the time until then to really hone in on the problem sections. Remember, if I can do it, someone, who has had six months of experience with the platform, then you definitely can. Don’t let anyone tell you otherwise. Now, go get that certification! 

We recently shared tips on how to study for the Salesforce Nonprofit Cloud Consultant certification exam and plan to share more soon. Want to join us? Checkout Oktana Careers.

How to Translate Salesforce Custom Labels Cheat Sheet

Custom Labels are custom text values that can be accessed from Apex classes, Visualforce pages, or Lightning Web Components and allow developers to build multilingual applications to provide a localized user experience.

We have a more detailed guide on localization with Salesforce Custom Labels, but below is an easy cheat sheet covering the basic mechanics. You’ll learn how to easily translate all Custom Labels from any application into a new language, whether translating with Google Translate, like below or through a human translator.

Salesforce Custom Label Translation Cheat Sheet

1. First, on your org code directory, go to src/labels to download CustomLabels.labels. Now go to src/translations to download the most current and complete translated language, for instance, fr.translation.

2. Rename both files as .xml.

3. Open MS Excel (at least 2007 version) and select the Developer tab. Go to XML, click on Import button and select the CustomLabels.xml file.

4. Delete all columns except ns1:fullName and ns1:value.

5. Filter column ns1:fullName with Contains Labels.

6. Select columns ns1:fullName and ns1:value (and its rows), then go to Find & Select. Go to Special, select checkbox Visible cells only, and then click the Ok button.

Now you can copy only the rows that are displayed within the filter.

7. Open Google Docs and choose the Blank Sheet option.

8. Paste copied rows directly into the Google Sheet.

9. Select all columns, then go to Data->Sort Range and select to order first by ns1:fullName and second by ns1:value. We have to do this because we need at least one of the label columns to be sorted so that when we are looping through the other set of data, we are sure we are looping all possible labels.

10. Add a new header to ja.translation.xml file, name it Translation. Add in the first cell =GoogleTranslate (<ns1:value cell>, “en”, <CodLeng>). For example: =GoogleTranslate(B2, “en”, “ja”), then copy or drag the cell until all the rows have been translated.

11. Copy all Google Sheet columns and paste them at the left of the Excel sheet fr.translation.xlsx like this:

12. Add a column in which we will match label cells hence the translation) from both tables with this formula: =VLOOKUP($G2;$A$1:$C$1 91;3;FALSE). The $ character is used to fix a column or a row depending on its placement. (If it is before the letter, that column will be fixed despite copying the formula to other columns. If it is before the number, you are fixing the row.)

  • $G2 is the first cell from the right dataset which contains the first custom label name and will loop through $A$1 up to $A$191 (in this example), searching to match labels.
  • The range ($A$1:$C$191) is fixing the column and row of the left set of data.
  • 3” indicates which value from the range of data we would like to copy when there is a match in the search, in this case, it will be doing so from the Translation column.
  • FALSE” forces that the formula only retrieves a match by exactly matching the label names.

13. Delete those cells where the formula returns #NA (meaning, there was no match between label names).

14. Where a translation was returned, replace the right dataset cell from column ns1:label with this value.

15. Once done, delete the left dataset (A to C columns) as seen in the above screenshot leaving ns1:label as Column A.

IMPORTANT: ns1:label2 and ns1:name3 are created for custom tabs values. We need to translate these, save the translation somewhere else and delete them from ja.translations.xlsx. 

We will later add them to the ja.translations.xml this way:


16. Save the Excel sheet, go to Developer:

17. Click on Developer:

18. Click on Export and save the file as <CountryCode>.translation.xml.

19. Remove the .xml extension.

20. Add any customTabs deleted together with their translations as described in Step 15. 

And that’s it! Learn more from our team, or check out our services.

Python and Salesforce

Today’s post is about, you guessed it, using Python and Salesforce together. Salesforce offers a few different REST API endpoints that allow us to interact with our org. They also offer a SOAP API, but we’re not going to use it today. In fact, we don’t need to worry about the endpoints at all. Thanks to a Python library called Simple-Salesforce.


We could do this all by hand with the built-in requests library. You would have to handle sessions, OAuth process and save tokens, deal with request headers, encoding and decoding JSON, creating configs to handle all the different endpoints, etc…

Simple-Salesforce Python Library

This is where the wonderful world of open source software comes to the rescue. A man named Nick Catalano created a library called simple-salesforce. From what I understand, he isn’t actively developing it anymore, but due to open source, the community has picked up adding features. It has about 50 contributors as of July 8, 2020. The lib is being actively developed, with new features added like bulk API support and formatted SOQL queries!



With a bit of the background out of the way, let’s start digging into the library and see what we can do with it. First, no better way to explain what simple-salesforce is than to quote the README itself:


Simple Salesforce is a basic REST API client built for Python 3.3, 3.4, 3.5, and 3.6. The goal is to provide a very low-level interface to the REST Resource and APEX API, returning a dictionary of the API JSON response.

Simple Salesforce


In plain text this quote means Python 3.x is supported, REST requests are handled for us, and the data we want comes back as a native Python data type. Great! Just what we want.


Login Information


First up, we’ll need a few things from Salesforce. Three things to be exact. Our username (email), password, and security token. The username and password should be pretty easy to find in your profile settings in your Salesforce org, but where do we find the security token? We don’t really see it, rather we’ll need to reset it.










After clicking “Reset Security Token” , you should be sent an email containing your new token. Save this, we’ll need it in the next steps. That’s all we need from Salesforce to get up and running. Now that we have that, let’s start building our Python script and start playing with our org.


pip and pipenv

But first, a quick word about PIP. While working on this example, there was an update to simple-salesforce lib. Github has the current version and we need the format_soql method from it. But pip (PyPi) hasn’t been updated with the new version as of yet, July 2 2020. So, we’ll need to install it via it’s repo on Github.


pipenv install -e git+

If you are using the demo repo I built, we won’t need to worry about the requirements or dependencies if using pipenv. The demo has a Pipfile that’s pulling from the repo already thanks to the magic of pipenv.

The code

Now, let’s write some code. First up, we’ll bring in the libs we’re going to use. The only one that is not part of the standard lib is simple-salesforce:

from simple_salesforce import Salesforce, format_soql

from pprint import pprint
import json

Simple enough, import simple-salesforce, pprint (pretty print), and json. pprint is only used to make the terminal output look better, so it’s not needed for the core examples. json is used to get the credentials from a .json file, which is what we’ll do next.

# open file holding our login information
# we have the login info in a separate file so we can
# add it to .gitignore to help prevent leaking the information
# environment variables could also work
with open("login.json", "r") as login_file:
    creds = json.load(login_file)

Keep credentials secret

If you were on a project working in a private repo and multiple people needed access to the login credentials, keeping login info in the script itself would be okay, not ideal but okay. The reason we’re doing it is because it’s good practice and it’s open to the public on Github. So hiding and not committing sensitive information is important. This goes for any language or code base.

With that disclaimer out of the way, I included an example login.json file called very creatively “EXAMPLE_login.json“. Let’s take a quick look at it.

    "login": {
        "username": "",
        "password": "My-REALLY-amazing-password",
        "token": "kdjfghdgfFGJbDFgd36DFGHDfgh"

Very simple json object only containing the three things we got from the last steps. You can just copy/paste the “EXAMPLE_login.json” and rename it to just “login.json“, then update it with your login information. You can do this in the file explorer or VSCode, but here’s a quick example to do it from the command line.

cp EXAMPLE_login.json login.json
vim login.json

Salesforce Object

With our new login information, we can create the salesforce object back in our python script.

sf = Salesforce(username=creds['login']['username'],

And that’s it! We now have an object that represents our org, and now we can start doing cool things like SOQL or DML. Next, since we have everything we need to start awesomeness, let’s try a simple SOQL query.

SOQL Query

# an example of running a simple SOQL query
SOQL = "SELECT Id, Email FROM Contact"
data = sf.query(SOQL)
for d in data['records']:
    pprint(f"{d['Id']} -- {d['Email']}")

We defined the query string to get the Id and Email from all the Contacts, called the query() method of our salesforce object, then looped through the returned records and display the results. A note to those new to Python, in the pprint() we use something called an f-string or format string. It makes it easier to embed variables in strings, much like the way Aura Components handle expressions with {!v.my_var}

SOQL is cool and all, but what about adding new data to our org? We can do that too, very easily. We’ll even try using the bulk api to insert 1,000 record. But first we need to create 1,000 records. This is going to be mock or fake data just for the sake of simplicity. We’ll also be testing on the Account object, so only thing required for new records is the Name field.

data = []
for i in range(0,1000):
        "Name": f"Bulk Test {i}"

Now we have a list of dictionaries that represent our record data. Here we have a for loop filling a list with new items. We could also use list comprehension to replace these 5 lines of code with just one.

data = [{'Name': f"Bulk Test {i}"} for i in range(0, 1000)]

Bulk Insert

To bring these new records into Salesforce, we use the .insert() method for the object we want, coming from the salesforce object we created. Confused yet? Here’s the insert code, it should help make things more clear.

# insert the new account using the bulk api
x = sf.bulk.Account.insert(data, batch_size=10000, use_serial=True)

Here, we’re telling our org we want to use the bulk api “sf.bulk“, then which record object we’re working with “.Account.“, and finally what we want to actually do “.insert()“. We could use any object too, doesn’t have to be just Account. Even custom objects work, so instead of where Account is, we can replace it with something like sf.bulk.MyCustomObj__c.insert(..... We can also specify the batch size, or to process in serial.


If you visit your org and take a look at all Accounts, you should see 1,000 new accounts with names like “Bulk Test 42”. We can also try doing another SOQL query, this time we’ll use the bulk api for the query. We’ll also show how to use things such as “LIKE” in SOQL statements.

# now lets get those records so we can delete them
SOQL = format_soql("SELECT Id, Name FROM Account WHERE Name LIKE '{:like}%'", "Bulk Test")
the_accounts = sf.bulk.Account.query(SOQL)

Simple-salesforce now comes with the handy little method called format_soql that allows us to do things such as LIKE. format_soql is also the reason we used the Github repo for pip instead of what’s on PyPi, the new method was just introduced in the last 2 weeks.

Now that we know we can insert and add new data and records to salesforce, let’s see about removing it. Looking at the README, seems like the delete() method needs a list of records containing the Id of the record to be deleted. The records need to be a key-value pairs or dictionary just like how the query was returned. We already have all the Id’s for our inserted record from the “bulk.query()

{'Id': '0013h00000EdP87AAF',
  'Name': 'Bulk Test 998',
  'attributes': {'type': 'Account',
                 'url': '/services/data/v42.0/sobjects/Account/0013h00000EdP87AAF'}}]

Looks like simple-salesforce also returns something called “attributes“. This just tells use which object we’re working with and which api endpoint we got the information from. For our example, and for the .delete() method, we only need the Ids. So let’s clean up and make a new list with only what we want.

account_ids = []
for a in the_accounts:
    account_ids.append({"Id": a["Id"]})

Here is another opportunity to practice list comprehension! See if you can get that for loop down to one line.

Bulk Delete

So now we have the list of Id’s, now we just simply call the delete() method and go refresh our org’s Account list.

data = sf.bulk.Account.delete(account_ids, batch_size=10000, use_serial=True)

All the records we inserted are now gone!

Final Thoughts

In conclusion, let’s recap what we’ve learned. For one, Python is awesome. Second, integrating Salesforce in a Python project is very very simple. With only three pieces of information, we can create a native Python object representing our Salesforce org, do things with the object like SOQL and DML, and finally have access to multiple salesforce API such as bulk or search.

This is only scratching the surface. We can do much more and simple-salesforce also has methods for metadata and describe. From this basic example, we could bring in Salesforce data to Flask API’s we build or insert new data to Salesforce from a data scraping crawler we make in Python.

We can also harness the power of Python’s ecosystem of visualization and reporting such as Pandas or SciPy. Nothing stopping us from grabbing Salesforce data and running it through machine learning or neural networks using PyTorch or TensorFlow.

If you’ve made it this far, thanks for reading! Here I’ll link the demo repo containing all the code discussed in this post and a YouTube video of my presentation. Also, if you are interested in learning more about Python, you can check this article: How to do Time Series Analysis with Python & Pandas.