Skip to content

Instantly share code, notes, and snippets.

@gbutt
Created May 19, 2019 18:40
Show Gist options
  • Save gbutt/1274f5d9af1f34f33e49c3127f521e8c to your computer and use it in GitHub Desktop.
Save gbutt/1274f5d9af1f34f33e49c3127f521e8c to your computer and use it in GitHub Desktop.
SFDX script to retrieve profiles and perm sets with object CRUD and FLS
#!/bin/bash
TARGET_ORG=$1
if ["$TARGET_ORG" == ""]; then
echo Please specify a target username
echo For a list of available usernames use: sfdx force:org:list
exit 1
fi
rm -rf temp
mkdir temp
cd temp
# package.xml
echo '<?xml version="1.0" encoding="UTF-8"?>' > package.xml
echo '<Package xmlns="http://soap.sforce.com/2006/04/metadata">' >> package.xml
# Profiles
sfdx force:mdapi:listmetadata -m Profile -u $TARGET_ORG -f Profiles.json
echo "<types>" > Profiles.txt
cat Profiles.json | jq '[.[].fullName] | sort | .[] | "<members>" + . + "</members>"' -r >> Profiles.txt
echo "<name>Profile</name></types>" >> Profiles.txt
cat Profiles.txt >> package.xml
# PermissionSet
sfdx force:mdapi:listmetadata -m PermissionSet -u $TARGET_ORG -f PermissionSets.json
echo "<types>" > PermissionSets.txt
cat PermissionSets.json | jq '[.[].fullName] | sort | .[] | "<members>" + . + "</members>"' -r >> PermissionSets.txt
echo "<name>PermissionSet</name></types>" >> PermissionSets.txt
cat PermissionSets.txt >> package.xml
# Custom Objects
sfdx force:mdapi:listmetadata -m CustomObject -u $TARGET_ORG -f CustomObjects.json
echo "<types>" > CustomObjects.txt
cat CustomObjects.json | jq '[.[].fullName] | sort | .[] | "<members>" + . + "</members>"' -r >> CustomObjects.txt
echo "<name>CustomObject</name></types>" >> CustomObjects.txt
cat CustomObjects.txt >> package.xml
echo '<version>45.0</version>' >> package.xml
echo '</Package>' >> package.xml
# retrieve metadata
sfdx force:mdapi:retrieve -s -k package.xml -r ./ -u $TARGET_ORG
unzip unpackaged.zip -d $TARGET_ORG
rm unpackaged.zip
cd ..
mkdir retrieve
mv temp/$TARGET_ORG retrieve/
rm -rf temp
@sfdcale
Copy link

sfdcale commented May 21, 2019

if ["$TARGET_ORG" == ""]; then should be if [$TARGET_ORG == ""]; then
It worked fine. Thanks

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