Salesforce Automation

This is probably a little sales specific but since I spent some time writing some automation figured I’d post it. I was tired of having to go through all of my open opportunities in Salesforce (SFDC). The lightning version that is being pushed out is SOOOO SLOWWWWW. To the point of being almost unusable (for crazy people like me anyways). So it was time for some automation. The simple-salesfoce python package is the perfect thing. The repo is here: Once I found my actual SFDC username I was good to go.

Our SFDC uses SSO so I had never used my real user name. To get your username first click on the little person icon in the top right and then click settings
Under the Personal Information section you will find your username
I also needed to set my password for access without SSO. There is a Reset Password link on the page.
You will also need a security token. There is a Reset Security Token link as well.
Access Steps

All of this info is needed each time you login so keep it handy. To get started install simple-salesfoce with pip. To get logged in do something like this:

from simple_salesforce import Salesforce
import getpass
sfusername = input("Enter SFDC Username: ")
sfpassword = getpass.getpass("Enter Password: ")
sftoken = input("Enter SFDC Token: ")
sf = Salesforce(username=sfusername, password=sfpassword, security_token=sftoken)

Now you can access all of the SFDC obects you have access too. One of the more powerful parts of the module is SOQL. SOQL is Salesfoce Object Query Language. SOQL is a SQL like query language for access into the SFDC data. Since my instance is already configured many relationships have been built and the “tables” are all already linked together. Because of this complex queries are easy:

sf.query(format_soql("SELECT Id, Name, Account.Name,CustomField1__c,CustomField2__c from Opportunity WHERE Account.CustomField3__c IN {territoryId} AND IsClosed = FALSE AND Record_Type__c = 'MANAGED' AND (CustomField1__c = 'In Progress' or CustomField1__c = '')",territoryId=sfTerritoryId))

I’ve modified this to obscure some of the custom field names. This query find all opportunities that require and update from me that are in my territories. sfTerritoryId is an array of my assigned territory Id strings. format_soql takes the array and inserts the values into the query with an OR logic. You’ll need to import this if you want to use it from simple_salesforce.

With this data i can now loop through all of the opportunities, apply more logic, and update them. This took an hour long activity and turned it into a less than a minute script run. Now I’m not adding anything useful but I am changing the date of the last update. I filter on records with no updates for a certain amount of days then add “No Update” to the notes field along with a date and some other internal info. Updating is easily accomplished.


Now for records where I have no updates I don’t have to manage them in the web interface. For meaningful updates I still use the web interface. This can be included in the script and may be in the future but for now I’ve saved myself more time than it took to write this post. This is inline with something I told someone else recently “Do what you like to do and automate the things you don’t like to do.”

The actual code includes to many internal names and fields to share publicly so no github link this time.

One thought on “Salesforce Automation

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: