Salesforce CLI

One of the smallest but very interesting things I saw during this years Dreamforce was a small open source utility called CLI. It is a small tool that you can use to interact with your instance from a shell or a prompt. You can do a lot of different things like running SOQL queries, anonymous APEX and create and modify objects.

You may ask why this is cool or useful at all – why use a command line utility when you can use a nice web interface? One thing is of course the speed and easy of a shell utility especially if you are used to work with tools via the command line or fluent in batch script creation. I see a lot of potential with this little tool and would like to give you one example application for it.

One thing that you might have seen is that you cannot deploy a class that is already in use by a schedule. This is especially annoying if you are using automated deployments using ant. You can of course remove the schedule using the interface and then add it after you are done. But this would not be a good solution if you want to do nightly builds from a GIT repository to a development or QA sandbox. You would then need to write a tool that is connecting using one of the APIs and disable your schedule and then re adds it for you again. You can now do that using the CLI without needing to write a lot of code. This is how it can be done.

I downloaded the compiled binaries from here – but could not get the OAuth handshake to work using firefox on my Ubuntu 13.10 machine. I knew that there was a fork of the code on GIT hub which allowed me to use SOAP username and passwords instead of a OAuth token which also makes a little bit more sense in our scripting scenario. I built that fork on my machine using the following commands.

export GOPATH=~/tmp
go get -u
cp ~/tmp/bin/force ~/bin

I choose to store the compiled binary in a bin folder in my home directory. If you are reading this some time after this was written this functionality might have been merged into the mainline and you would not need to build yourself.

Do you like the content on this page? Sign up for our newsletter
to make sure you are not missing out!

If you run the tool you will have the following list of commands to use.

Usage: force <command> []
Available commands:

login Log in to
logout Log out from
whoami Show information about the active account
sobject Manage sobjects
field Manage sobject fields
record Create, modify, or view records
fetch Export specified artifact(s) to a local directory
export Export metadata to a local directory
import Import metadata from a local directory
query Execute a SOQL query
apex Execute anonymous Apex code
version Display current version
update Update to the latest version
help Show this help

In this article we will be focusing on the query and apex commands to be able to delete and schedule a job. Before we tie it all together I will demonstrate a few of the key pieces we need to do this.


First we need to do a login against the org. To do this you will be using the following format login <url> <username> <password>

<url> will be the for a production or developer org instance and for a sandbox instance. <password> will be the user password with the security key suffixed if you need to use a security key in your specific org.


The be able to get the job Id of the scheduled job we need to do a SOQL query against the org. You will be using the query command for this and we will be looking at the CronJobDetail and CronTrigger objects. A quick example of the query command could be

force query "SELECT Id, Name FROM Account"

To give you a list of all the Id and Name of the Account’s in your org.


To create and deactivate the schedules we will use anonymous APEX. Using

force apex

will give you the possibility to enter code but you can also feed force using stdin to be able to script the call.

BASH Scripts

Using these commands and some basic bash scripting I was able to build two scripts; one to disable the schedule and one to recreate it afterwards.


~/bin/force login XXX XXX

echo "ScheduleImports p = new ScheduleImports();

String sch = '0 0 1 * * ?';

system.schedule('Scheduled Import', sch, p);

" | ~/bin/force apex


~/bin/force login XXX XXX

# Find the ID

JOBID=`~/bin/force query "SELECT Id FROM CronTrigger WHERE CronJobDetail.Name = 'Scheduled Import'" | sed -n 3p | tr -d ' '`

if [ $(echo $JOBID | wc -c) != '19' ]; then

echo "Couldn't find the JOBID"

exit 1;

echo "system.abortJob('$JOBID');" | ~/bin/force apex

Please note these scripts are only for demonstration – to be used in a production environment they should be polished. I hope this sparkles some inspiration to what you can do with this tool.

This blog post is a repost from Martin’s old blog called webaholic.

Leave a Reply

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