I found Salesforce packages to be very useful in resolving dependencies while metadata migration and this comes handy when we are deploying between orgs where we cannot use changeset or due to complexity in project we have to migrate metadata manually.
Please note that this is not the use case for continuous integration, where our package.xml file is maintained in a better manner.
The following approach will generate the package.xml, all metadata files with all dependencies in minutes and you will be all set for deployment to the target org. For obvious reasons, if there are conflicts with the metadata and the target org, that has to be resolved manually, on case to case basis.
Step 1: Create an unmanaged package with all the components in your target org.
Go to Setup > Package Manager > New
Give a name to your package (testpackage in my case) and save
Click Add to add components
Please note that you need to add only key components, and all the related components will be added to the package automatically.
For example, if you add an App, all Objects included in that app, with related custom fields, apex triggers, Apex classes, visualforce pages (if used as button overrides/in inline page in page layout), page layouts, custom settings etc. will be automatically be added.
For the purpose of this activity you don\’t need to upload the package.
Step 2: Install SFDX in your computer. Here are the instructions
Step 3: Open Terminal if you are using Mac or Command Window if in Windows. Please be sure to add location SFDX to your path. Go to your workspace directory and make a directory named deployment and cd to that directory
Step 4: Connect your Source Org. Change login.salesforce to test.salesforce if you are using a Sandbox. Change to custom domain if you have if enabled and login is restricted outside of domain.
[code language=\”javascript<span data-mce-type=\”bookmark\” id=\”mce_SELREST_start\” data-mce-style=\”overflow:hidden;line-height:0\” style=\”overflow:hidden;line-height:0\” ></span>\”]
sfdx force:auth:web:login -r https://login.salesforce.com -a sourceOrg1
[/code]
This will open a window in your default browser with login page. Login, authorize and close your browser window once successful. This is what you will see in your terminal,
Step 5: Similarly Connect your target org,
[code language=\”javascript\”]
sfdx force:auth:web:login -r https://login.salesforce.com -a targetOrg1
[/code]
Step 6: Extract metadata from Source
[code language=\”javascript\”]
sfdx force:mdapi:retrieve -s -r ./ -u sourceOrg1 -p testpackage
[/code]
This will generate package.xml with details of all fields and also download all metadata. Please note that this will generate a zip file. Unzip and modify if you need to,
Step 7: Deploy metadata to target,
Command for deployment if you have unzipped it and the target is a directory,
[code language=\”javascript\”]
sfdx force:mdapi:deploy -d ./unpackaged -u targetOrg1 -w 10
[/code]
Command for deployment if you are deploying the zip file,
[code language=\”javascript\”]
sfdx force:mdapi:deploy -f ./unpackaged.zip -u targetOrg1 -w 10
[/code]
You may use -c option to to validate only instead of actual deployment.
Here is the deployment status in target org,