Press enter to see results or esc to cancel.

How to automate the Regular stuff using the SFDX?

In this blog post, we will see how to automate regular stuff using the Salesforce CLI. The main advantage of using CLI is scriptability, you can automate the regular tasks by using any of the scripting languages available and CLI. Before proceeding further I am hoping that you already have an understanding of Salesforce CLI. Please check my blog posts that covers basic understanding of Salesforce CLI.

 

Prerequisites: 
  1. Salesforce CLI installed on your machine. 
  2. OpenSSL installed on your machine ( We need a certificate to create a connected app )
  3. The basic idea of any scripting 
  4. DevHub enabled org
Salesforce CLI can be installed from the link below. Please download the file based on your machine preference.  https://developer.salesforce.com/tools/sfdxcli
 
The script we are going to use looks like this.
 

automation

 

 
Step by Step guide to create a Self-Signed Certificate using OpenSSL:
 
Install OpenSSL on your machine if not already done.
 
I am going to use a shell script for this blog post. Please have a Dev Hub enabled org ready with you.  We will create a certificate that we are going to use while creating the Connected app. The whole point of having this certificate is while authenticating the DevHub org we are trying to eliminate the Credentials.
 
We will provide the clientId of the connected app we are going to create.  I hope at this point you have already installed the OpenSSL on your machine, If not please do that.  Please check this documentation on how you can use the OpenSSL command to create a self-signed digital certificate.
 
 

I am going to use them to generate the self-signed certificate. Make sure you create a new folder and perform the operation there.  The below commands will be used to create a private key called server.key

Then observe that there will be two files in your folder from where you are performing these operations.

server-key
Let’s execute another command which will create a certificate signing request file by using the server.key file.
 
NOTE: There will be some details we need to enter in the command prompt while executing this command. Please follow the instructions as shown in the command prompt.
 
 
By using the CSR file generated above we are going to create a certificate file. Please execute the final command.
 
Once you execute the final command please observe at the end you will have four files as below. 
total-files
Then we will go ahead and create a connected app in Salesforce by using the certificate we just generated.


Step by Step guide to create a Connected App:

  • Go to setup 
  • From the search look for App Manager
  • Click on the New Connected App button from the top Right Side 
  • Give it a name as “OpenSSLDemo”
  • Enter Contact Email 
  • Then under API(Enable OAuth Settings) enable the checkbox “Enable OAuth Settings”
  • Enter the callback URL as “http://localhost:1717/OauthRedirect
  • Then Enable Use digital signatures checkbox.
  • Now select the Choose File button to load the certificate you just created. 
  • Under the Selected OAuth Scopes select all below mentioned OAuth Scopes
    1. Access and manage your data (api)
    2. Perform requests on your behalf at any time (refresh_token, offline_access)
    3. Provide access to your data via the Web (web)
  • Click Save. It will take you at least 2-10 minutes to reflect the changes.

Manage-Connected-Apps-Salesforce

  • Click Continue. You will be landed in OpenSSLDemo connected App. 
  • Click Manage button → Edit Policies Under OAuth Policies please select your preference for this blog post I am selecting “Admin approved users are pre-authorized” then click save
  • Finally, create a permission set and assign the permission set to users who need the Connected App access. ( In the permission set under the Assigned Connected Apps select OpenSSLDemo and save assign this permission set users who need access to this Connected App).

edit-policies

 
Then you are set to use the connected app in the JWT Authentication Flow.  Most of the part is completed now we will check what can be automated. I will cover a few scenarios but it’s open to individual imagination ( Requirement ).
  1. Cloning the Github repo ( I am going to use dreamhouseapp ) ( https://github.com/dreamhouseapp/dreamhouse-sfdx.git )
  2. Changing the location to newly created Repo
  3. Authorize the DevHub org using the OAuth JWT flow
  4. Creating the scratch org using the just now authorized DevHub org
  5. Pushing the dreamhouse metadata to scratch org created just now
  6. Assigning permission set in order to see the dreamhouse application
  7. Loading the data into Brokers and Properties
  8. Opening scratch org
Save the above file as automation.sh and we will execute the file to see the output. Before doing that I hope the above code is self-explanatory and I have used session-based variables to have the commands look cleaner. Get the consumerkey from the Connected application we have created above.

Home-Salesforce

Thanks for reading the post. Please comment below if you have any questions/suggestions related to the blogpost.