In the previous blog post we have seen how to setup the Visual Studio Code and install some required extensions. Now we will see how to work with metadata in Salesforce, We are going to cover below topics.
- Metadata Retrieve & Convert
- Source Retrieve & Convert
- Create Metadata (LWC Component)
- Source Deploy
- Metadata Deploy
In general when you create a project, Under the force-app you can see all the metadata present in different folders. This metadata is in source format. The regular format is called mdapi format. There are certain differences between source format and mdapi format.
These are some of the differences I can think of. There is good
stack exchange post explaining when to chose what, please refer to that to get more clarity on this topic.
In the previous blog post we have successfully connected to the org. If not already done authenticate your org in VSCode.
Command: sfdx force:auth:web:login –setalias VSCodeSetup –instanceurl https://login.salesforce.com –setdefaultusername
You can either use the cmd to perform these operation / You can use Integral Terminal that VSCode offers, Its your choice. I prefer to use VSCode Terminal.
Metadata Retrieve & Convert
Remember we have created the project with manifest file while creating the project. The advantage of using manifest is it will create the sample package.xml file inside manifest folder.
Now we will see how to retrieve the content from the package.xml to our local folder using mdapi format.
Create a folder in VSCODESETUP project folder with name metadata.
Execute the command : sfdx force:mdapi:retrieve -k manifest/package.xml -r metadata
Lets see what this command does, it will check for the manifest folder in that package.xml file then it will retrieve the metadata mentioned in the package.xml file to the folder metadata.
In case if you need help building the command please take the help from the terminal by appending the command with –help/-h
Ex: sfdx force:mdapi:retrieve –help
By this time command executed successfully and retrieved zip with name unpacked.zip to metadata folder.
Now the format is in zip we need to unzip the folder. Either you can do it manually or using the command you can unzip the folder.
Execute this command inside the metadata folder tar -xf unpackaged.zip
Its up to you whether you want to keep the zip file or not. I will go ahead delete the zip file and move to the project folder.
Tip: To go one directory level up execute “cd ..”
This is one way of getting the metadata using the package.xml. We will how we can bring the metadata from org using changesets.
NOTE: To show this feature I have connected to different org, Created another folder metadata2 inside VSCODESETUP project folder.
Execute the command: sfdx force:mdapi:retrieve -p testPack -r metadata2
Now the command is self explanatory, In case if you want to know more help right away at you r finger tips with out leaving anywhere.
These are two ways you can retrieve the metadata from the org. Now will see how to convert the existing metadata to source format.
Execute the command : sfdx force:mdapi:convert -r metadata/unpackaged -d force-app
Again the command is self explanatory, it will fetch all the contents inside unpackaged folder and convert them to source format and place them in force-app default folder in the respective metadata folders.
Source Retrieve & Convert
Now that we have seen retrieving and converting the metadata from the existing org using the mdapi. But keep in mind that we can’t work with mdapi format in sfdx that is the reason we have to convert the mdapi format to source format.
We will see how to source retrieve work in case of change sets.
Execute this command: sfdx force:source:retrieve -n testPack
Now you can see a separate folder with name testPack created which contains all the related metadata in source format.
Remember we have used package.xml in the mdapi command. Lets use package.xml file too retrieve the source format.
Execute this command: sfdx force:source:retrieve -x manifest/package.xml
Lets convert the source format to mdapi format in case if you want to use the metadata format somewhere else.
Execute the command : sfdx force:source:convert -r force-app -d metadata4
It will convert all the files present in force-app folder to metadata4 folder in mdapi format.
Tip: In windows machine in case metadata4 folder is not present, System created the folder and moved the metadata there.
Till now we have seen Metadata Retrieve & Convert & Source Retrieve & Convert. Its time to create some metadata in the local org.
Create Metadata (LWC Component)
For this section we are going to create LWC component using the trailhead module
Execute this command: sfdx force:lightning:component:create -n helloWorld –type=lwc -d force-app/main/default/lwc
NOTE: I have deleted all the metadata folders we have created above to have a clean view. Once the command executed you can see all the files created as below.
helloWorld.html
helloWorld.js
helloWorld.js-meta.xml
Lets replace the content from trailhead.
In the same way we can create other metadata as well. Please check the commands for the rest of the metadata. You know how to check the commands available.
Source Deploy
Its time to push the changes we have made to the org we are working on. We can move the metadata changes in two ways either through Source format or mdapi format.
Let’s see how we can move the metadata using the source format.
Execute the command: sfdx force:source:deploy -p force-app/main/default/lwc/helloWorld
Verification can be done by dragging the helloWorld component to any of the App/Record page as defined in xml file. But for now I am showing the deployment status.
We can’t directly perform the metadata deploy on the source format.
NOTE:
If you are using source format you can directly deploy the code using source command. Instead if you want to use mdapi format first you need to convert the source format to mdapi format then you can deploy using mdapi commands.
I have created a sample apex class to demonstrate the Metadata Deploy.
Execute these command one after the other.
- sfdx force:source:convert -r force-app -d metadata
- sfdx force:mdapi:deploy -d metadata -w 10
Please Observe that mySampleClass created successfully.
With this we are covered most of the metadata operations we can perform using the Salesforce CLI.
Thanks for reading the blog post. Please drop a case comment for any questions/suggestions.
Hey there,
I am Saicharan working as a Salesforce Support based out of Hyderabad having nearly 5 years of experience in the Information Technology Industry and close to 2 years in Salesforce, Apart from the job I love music and obviously playing cricket. Giving back to the people is what matters most. #Ohana
Cheers !!!
Comments
1 Comment
[…] the previous blog post we have seen how to work with Salesforce metadata and most of the operations involved. Now we will […]
Leave a Comment