[SalesForce] Scratch org: how to retrieve metadata

Scratch org: how to retrieve metadata

I am probably missing some concepts, but this are the steps that I have follow to create a new scratch org from a developer edition org.

Created a new dev org
In setup >> Dev Hub >> I enabled Enable Dev Hub and Enable Unlocked Packages (GA) and Second-Generation Managed Packages (Beta)
From VS, I created a new project with manifest (authorize it, add alias, …)
From VS, I created a new scratch org, with an alias "firstattempt", this generated this success message:

11:14:07.787 sfdx force:org:create -f config\project-scratch-def.json --setalias firstattempt --durationdays 7 --setdefaultusername
Successfully created scratch org: 00Dp00000009ccFEAQ, username: test-ovvqjhrqkdcl@example.com
11:14:24.478 sfdx force:org:create -f config\project-scratch-def.json --setalias firstattempt --durationdays 7 --setdefaultusername ended with exit code 0

So this is where I am confused.

If I want to develop in this scracth org, I guess I need to create another project in VS?

I created a new project in VS, and set the default org to be the new alias created when the scracth org was created.

(I tested opening the default org and it opens in a new tab without having to authorize)
enter image description here

The problem is that I dont have an option to retrieve metadata

So I am guessing I need to authorize this new project?

But if I do this I will need to put the username and password in the web browser, and when the scratch org was created I only get a username, is there a way to authorize the org without doing this web auth?

Or I am doing something wrong and I dont need to create a new project when working with the scratch orgs?

Best Answer

A scratch org is a completely blank Salesforce org that's divorced from any data/metadata in any other org in your Salesforce universe (including the Dev Hub - your Dev Hub org just gives you the ability to create scratch orgs and managed them via the UI). So there's no metadata to retrieve from the scratch org itself.

If you want to pull metadata from a different org, like the Dev Hub org or a Production / Sandbox org, into your scratch org, do the following:

  1. First authorize the org you'd like to retrieve metadata from:
sfdx force:auth:web:login -a MyDevOrg
  1. Use force:source:retrieve to pull relevant metadata you're looking for from that org. See the documentation on all the options of how to pull specific metadata. A sample of how to pull all classes and triggers will look something like this:
sfdx force:source:retrieve -m ApexTrigger,ApexClass -u MyDevOrg 
  1. Push that metadata you just retrieved from your local project into your scratch org
sfdx force:source:push
Related Topic