function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brian CBrian C 

Copying custom objects & fields from sandbox to scratch org

What is the best method for populating a scratch org with custom objects and their fields from a sandbox? I have tried the following process and have been unsucessful- am I missing a step or doing something incorrectly?
  1. Within the sandbox, I've created an unmanaged package containing the custom object. I noticed that the package also automatically pulls in dependent classes, pages, etc. 
  2. I've pulled the package to my local environment using:
    sfdx force:mdapi:retrieve -s -r packages -u Sandbox -p objectexport
    
  3. I've unzipped the downloaded package.
  4. I've converted the package to DX format using:
    sfdx force:mdapi:convert -r packages\objectexportunzipped
  5. I then try pushing the converted object to the scratch org:
    sfdx force:source:push
The attempt errors out with dozens of "variable does not exist" and "invalid type" errors. From what I can see, it looks like the order the objects and classes are being pushed is causing the system to not see the dependent fields.

My end goal is to have a repeatable process to replicate custom objects from my sandbox to scratch orgs. 
prateek jainprateek jain
Hi
You can use  Changeset to move custom object And fields from sandbox to production
if any issues please do let me know  I will be happy to help
Corey CummingsCorey Cummings
prateek, it seems to me like we are supposed to be able to automate this development process using the sfdx CLI but I have the same trouble as Brian above. How do we make a scratch org the same structure and schema as our sandbox environment? There has to be a way to use our source control objects to perform a full deployment to a scratch org. I don't think anyone wants to manually create a changeset, manually pick each item in the changeset and manually upload and deploy every time they create a scratch org. I would love someone who has this working to give us some input because I'm stuck using the SFDX methods until we get this solved. Thanks for any input.
Brian CBrian C
Hi Prateek-

Thank you for your reply. As Corey has explained, the issue is with populating a scratch org with custom objects and settings. The ideal way would be an automated process. I'm not against some initial setup of a configuration file containing object data as long as I can reuse it, but I do not know how I would go about setting up such a file. 
xi xiao 3xi xiao 3
I have the very same puzzle, and created a stack overflow question https://salesforce.stackexchange.com/questions/213190/extract-custom-object-from-sandbox-into-scratch-org-and-version-control-system

I tried `sfdx force:mdapi:deploy` to deploy custom object into scratch org, it went well and the custom object is visible in scratch org.
However, `sfdx force:source:status` does not recognize the new custom object....

Anybody knows the answer?