Skip to content

Instantly share code, notes, and snippets.

@paulroth3d
Last active October 10, 2018 15:26
Show Gist options
  • Save paulroth3d/78bd25127b621599a75cd0929db5254e to your computer and use it in GitHub Desktop.
Save paulroth3d/78bd25127b621599a75cd0929db5254e to your computer and use it in GitHub Desktop.
SFDX metadataRefresh
#!/bin/bash
# refresh the mdapi folder - for the latest package.xml file
sfdx force:source:convert -r force-app -d ../mdapi
# retrieve the latest using the package
sfdx force:mdapi:retrieve -s -k ../mdapi/package.xml -r ../mdapiRefresh
# unzip the package so we can import it
sfdx dbs:zip:uncompress -s ../mdapiRefresh/unpackaged.zip -t ../mdapiRefresh/ -r
# import the files
sfdx force:mdapi:convert -r ../mdapiRefresh
#!/bin/bash
# find any duplicates and copy them
# so I can use the 'merge' command from DiffMerge
# and choose which changes to keep.
find . -type f -iname "*.dup" -print0 | while IFS= read -r -d $'\0' line; do
echo "duplicate found: $line"
dup="${line}"
nonDup=$(echo "${line}" | rev | cut -d '.' -f2- | rev)
#create the original
orig="${nonDup}.orig"
cp "${nonDup}" "${orig}"
#echo "${line}"
#echo "${nonDup}"
#echo "${orig}"
diffmerge -i "${orig}" "${nonDup}" "${dup}" -t1="orig" -t2="current" -t3="duplicate"
done
#!/bin/bash
find . -type f -iname "*.dup" -delete
find . -type f -iname "*.orig" -delete

Overview

Sometimes when you are working with SalesForceDX / CLI,
the changes you make in the org aren't included in force:source:pull

This can happen often with:

  • page layouts
  • reports
  • etc.

TLDR;

Assumes:

  • /
    • mdapi
      • package.xml
      • other nonsense
    • dx
      • force-app
      • ...

1. Export your Salesforce DX files to a MetadataAPI folder

ex:

# delete the entire folder 
rm -rdf ../mdapi
# use sfdx to convert the DX folder to
sfdx force:source:convert -r force-app -d ../mdapi

2. Use the CLI to retrieve a new folder using the generated package

ex:

# use the package of the newly converted package to retrieve
# mdapi will always get the latest - so bypassing `force:source:pull`
sfdx force:mdapi:retrieve -s -k ../mdapi/package.xml -r ../mdapiRefresh

3. Unzip the CLI results

ex:

# I use my old cli plugin to make it cross platform
# https://github.com/paulroth3d/sfdx-dbs-plugin#uncompress-a-folder
# but any unzip command is fine
sfdx dbs:zip:uncompress -s ../mdapiRefresh/unpackaged.zip -t ../mdapiRefresh/ -r

4. Import the freshly retrieved metadata back to the DX folder

ex:

# import the newly retrieved metadata back to the dx folder
sfdx force:mdapi:convert -r ../mdapiRefresh

5. Review and remove duplicate files

Typically I do this with two commands:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment