DEV Community

Javier Garcia
Javier Garcia

Posted on • Originally published at manzanos-tech-blog.ghost.io

Automating Salesforce deployments with PowerShell

Recently I've been learning a little bit of PowerShell, initially for the sake of automating some tedious tasks I had to do at work like calling a REST endpoint 10-20 times to populate some DB for testing or simply pipelining some CLI commands.

The thing is, as I got more comfortable with PS, I started to realize the huge amount of things that can be done with scripts which make our day to day easier. The biggest one that came to my mind was Salesforce deployments.

Usually, once we've finished developing a certain feature in Salesforce, we run all tests, pull all our changes from the environment where we were developing (In Salesforce, you develop remotely, not locally), deploy to a staging environment and run again the tests there.

The issue is that that process involves a lot of manual steps. So I decided to try and automate it. For it, I looked into the SFDX CLI and tried to reuse most of its features: an authentication command, the actual command which calls the deployment endpoint, etc.

Eventually, after a few hours, the result was this:

It's a little nifty script which prompts a tab in your default browser so you log into the destination org and pipelines the deployment and the running of tests with real-time status in the console. That I did simply by polling the API.

There are, of course, many things to be improved in the script but, as a starter, it's already saving me a lot of time! :)

Top comments (2)

Collapse
 
alanmbarr profile image
Alan Barr

Powershell is awesome, you might look into named parameters and validations on those parameters saves a lot of headaches. I typically lean toward using Write-Output instead of Write-Host but that might be a personal preference.

Collapse
 
manzanit0 profile image
Javier Garcia

Yeah, thanks for the heads up Alan :)
As for the Write-Host, the main reason to use it is the colors. Since it's mainly a script to be executed by the user from the console, I thought the colors would be nice. But otherwise, just like you said, Write-Output is always better.