Retrieve and deploy using SFDX

There are several script options available in market to perform retrieve and deploy in Salesforce. Eventually, all of them usage Salesforce Metadata API. What if I say, Salesforce has it’s own tool to perform all such operations. Yes, you read it right. SFDX has all such capabilities encapsulated in single unit. However, before you go through this blog, I would recommend you go through my previous blog related to SFDX, to install essential software required.

Alright, you should be good now. Let’s go step by step:

1. Authenticate Your Org

In order to authenticate, use below SFDX command:

sfdx force:auth:web:login

Few important command attributes:

-a, –setalias – You can set alias for the authenticated org. So next time, if you would like to perform any operation in given org, just use alias instead of authenticating it again. So unlike ANT migration, you don’t need to authenticate each time you wanna access given org with SFDX using alias. This is the beauty of SFDX.

-s, –setdefaultusername – set the authenticated org as the default username that all commands run against.

-r, –instanceurl – the login URL i.e. login.salesforce.com, test.salesforce.com, of the instance the org lives on.

You can check more by executing following command:

sfdx force:auth:web:login --help

Let’s execute login command with alias

sfdx force:auth:web:login –setalias MyOrgAlias

It will open a browser and ask for login. Once login, check your command prompt – you will notice a message saying – “You may now close the browser.” That means, you are successfully authenticated.

let’s move to the next step.

2. Retrieve your Metadata

Once authenticated, next step is to pull the metadata. Execute following command to retrieve metadata:

sfdx force:mdapi:retrieve

Few essential command attributes:

-r, –retrievetargetdir – Directory root for the retrieved files. This is required.

-u, –targetusername – username or alias for the target org. Use this attribute to tell command, which org you are working with. You can set the you just need to provide alias name you set initially at the time of login.

-w, –wait – if you are a developer, this is as good as setting timeout. This is a wait time for command to finish in minutes.

-k, –unpackaged – This is a file path for your package.xml file. i.e. src/package.xml

You can check more by executing following command:

sfdx force:mdapi:retrieve --help

After executing above command, wait for status as succeeded. Once succeeded, check your directory and you should have metadata retrieved.

So here is a last one.

3. Deploy your Metadata:

Here is script for deploy

sfdx force:mdapi:deploy

and as usual useful attributes:

-c, –checkonly – this attribute is for validate only.

-u, –targetusername – username or alias for the target org. Use this attribute to tell command, which org you are working with. You can set the you just need to provide alias name you set initially at the time of login.

-d, –deploydir – Root folder of your project.

You can check more by executing following command:

sfdx force:mdapi:deploy –help

And that is all my friend. #HappyCoding