3

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: [email protected]
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?

3 Answers 3

7

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
4
  • Thank you Brian, I am a bit more confused now, I thought the puropse of the scratch org was to develop in it (create code) and once I am happy with the development then push this code to a different org, but I think u understood to do the opposite? code where the scratch org was "born: and then push this into the scratch org?
    – manza
    Commented Apr 4, 2019 at 4:23
  • 1
    This deserves a whole post, but on one foot - development happens on your local machine in a DX project. From there, you have two main options - 1) push/pull/develop against the scratch org, which keeps source control, compiles code/settings in the exact environment setup on the scratch org; 2) push/pull/develop against a sandbox, much like the process that people know from MavensMate; (or 3 - both together). Scratch orgs has inherent source control and mirrors git, but you can achieve that paradigm just connecting your DX project to Github and do development in a sandbox Commented Apr 4, 2019 at 6:41
  • Brian, I would modify your answer where you say 'divorced from the DevHub' - since you technically need a reference to a DevHub in order to create a scratch org. I find in my research that most people mark their production org as their DevHub, although not a requirement
    – bkwdesign
    Commented Jul 12, 2019 at 14:36
  • @bkwdesign Thanks for pointing out that unclear point. See the updated answer Commented Jul 14, 2019 at 7:12
0

sfdx can generate manifest for you that you can then use to retrieve

sfdx force:source:manifest:create --name package.xml --from-org [email protected] --output-dir manifest
sfdx force:source:retrieve --manifest manifest/package.xml -o [email protected]
-3

use this to Authorize on Sandbox Or scratch Org : sfdx force:auth:web:login --setalias myOrg --instanceurl https://test.salesforce.com --setdefaultusername

Use this For retrive Mtadeta : sfdx force:source:retrieve -m ApexTrigger,ApexClass -u myOrg

1
  • This is the same as the answer from ~3 years ago, but with much less detail.
    – Derek F
    Commented Jan 30, 2023 at 12:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .