(upbeat music)
- Hey everyone, my name is Mohith,
I'm Developer Evangelist here at Salesforce.
In this video I'm gonna show you
how to successfully retrieve org metadata
using Salesforce CLI and packages.
This technique is useful
if you are moving away from org based development model
to source driven development.
Source driven development uses Salesforce CLI
and scratch org or sandboxes
that have source tracking enabled.
I've installed Salesforce CLI.
You can install Salesforce CLI
by visiting developer.salesforce.com/tools/sfdxcli.
I've also created an org.
It's a developer org,
where I have deployed all the metadata
that we have created for one of our sample gallery apps,
Easy Spaces.
You can visit the URL
trailhead.salesforce.com/sample-gallery,
and take a look at the metadata
that we have for this specific application.
I've also installed Salesforce extension back
for Visual Studio code,
which you can obtain from Visual Studio Code marketplace.
I've also scaffolded a project,
an empty project using the command palette,
and by using the command SFDX Create Project,
and selecting the standard template.
I'm also connected to the developer org
where I have all the metadata
and I'm authenticated to that.
So in the org, I have lots of metadata.
I have classes, I have permission sets,
I have custom objects, fields,
and all of the metadata
that's part of this Easy Spaces application.
I want to extract all the metadata to my project workspace.
And the easiest way to do that
is
by
creating packages.
So you go to Package Manager,
and click on Package Manager
and New
and then provide a name for the package.
Now in this case, let's say,
I want to say Easy Spaces as my package name.
Now, important thing to note here is,
you have to make sure you add
the top level metadata elements
so that all the dependencies
for that specific application is pulled,
once I add this metadata to this package.
So for example, if I add a space management app,
because it is the top level metadata,
and it has all the dependencies to different fields,
objects, permission sets,
you will see that once I add the metadata,
it will bring all the dependencies in this specific package.
So all of the dependencies are automatically retrieved.
And that's the property of a package.
Now note that we are not gonna upload this package.
And if you delete this package,
it doesn't mean that all of this metadata
will go away from your org.
You are free to delete
and create as many packages as you like.
It's just an unmanaged package,
which essentially is a container holding all this metadata.
Now let's see how we can retrieve this metadata
to our project workspace.
So Salesforce provides command
called sfdx force:source retrieve command
to retrieve your metadata.
You can do a help on this command,
and it will show you
different variations of the commands that's possible.
Now because we are retrieving the metadata
from the package,
you can follow command,
sfdx force:source retrieve
and provide the name of the package.
In this example, my name of the package is Easy Spaces.
So I go ahead and provide a name for that.
And once I run this command,
this will pull all the metadata in my package
to my local directory.
Now, once the package is sort of retrieved,
you can see all the metadata in a folder
named with the package name,
and you can see all of the components of that package.
So,
unmanaged package containers
are really a good way to understand dependencies
between the component,
it can be a playground to create packages
and find dependency between different metadata
and retrieve them locally.
So for example, let's say
I wanna pick up one of the classes like customer service
and more add to the package,
it will not only just pull up that class,
but it will also pull all other dependent metadata
that this Apex class has
like customer fields,
like for example, we have custom fields.
And
we have
Apex class,
we have the page layouts.
So all of these are interdependent,
so we can do that.
Now the thing about this command is,
you can also
retrieve multiple
packages.
So it's not just only one package,
you can retrieve multiple packages,
and those will be retrieved in multiple directories.
So all we will have to do is make sure
that we provide package names,
as comma separated list,
and then we can retrieve multiple packages
right within your project workspace by using this command.
Now, oftentimes, we do not sort of remember this command,
and, it takes some time
to get used to typing these commands.
So Visual Studio Code provides
something called as tasks.json.
So in your .vscode directory,
create a file called tasks.json.
I will share this file with you in the description section.
So this file, I created just to show you an example
of how to use tasks.json.
So I have the command to retrieve the metadata
sort of return with arguments,
and then I bind the input,
so that, we can run these commands,
without having to type them.
So let's see how to run them.
So to run them on your
Mac,
you'll have to type Command + Shift + B,
on your Windows, you will have to type Ctrl + Shift + B.
so say Ctrl + Shift + B
and you will see the same command that we have
in the tasks.json sort of listed here,
and you select them and then you enter the package name.
So for example, in this case,
my package name is Easy Dependency.
So I just enter them and then say Enter,
and it will run the same command in the terminal
without me having to type this command.
So anytime I enter Command + Shift + B,
I'll get an option to execute the command
with the inputs that I provide.
So as you can see,
there is another folder sort of created
with all the metadata elements that have retrieved
and the folder name is equal to the package name.
You can use the same technique to retrieve metadata
from your managed packages or unlock packages
or change sets that you have built in your org.
Now, if you have also based customizations
on top of a managed package,
you can also retrieve those metadata
by collecting them inside the package
and then retrieving them by using the package name
as shown in this demonstration.
I hope this video gave you a good overview
on how to retrieve org metadata using packages.
If you learn something, be sure to like this video.
If you wanna get more content like this
push directly to you,
click subscribe and that bell
to get notifications.
(upbeat music)