Skip to content

Instantly share code, notes, and snippets.

@matt-newell
Last active June 28, 2019 16:08
Show Gist options
  • Save matt-newell/cd1e1caff90fa16158ebb006c31022e8 to your computer and use it in GitHub Desktop.
Save matt-newell/cd1e1caff90fa16158ebb006c31022e8 to your computer and use it in GitHub Desktop.
Validate Visualforce domain is ready

VALIDATE VISUALFORCE DOMAIN IS READY

Have you ever created a new scratch org with sfdx but seem to have trouble access visualforce pages? This seems to be a know issue and can take 0-18 minutes before the org is fully operational. This random delay can cause havoc on e2e/integration testing as well as unit test. So after many attempts to fix or delay CI/CD I believe I have finally found a solution.

First let's talk about the actual issue, spinning up a new scratch org. After running sfdx force:org:create you will receive a success message along with login/password info for your org. The core sfdc features of the org are almost ready instantly but if you have a managed package with namespace it takes 0-18 minutes. Navigating to one of these pages(vf pages) will not error with a 404 nor will they timeout. However it will load a blank screen so let's dig in a bit 😺

The domain name of your org is stored inside .sfdx/org and using get-visualforce-url.js will return the visualforce domain name for this org. Now running the dig command on this url should return something like cs66-iad.iad.r.force.com. Unfortunately during the 0-18 minute delay dig will return location.l.force.com. This means the data center your org is running on is not 100% ready.

So how do we fix this? Simple just add a step to local or CI/CD to run dig every 30 seconds until your scratch reports the correct data center.

Bob's your Uncle

const url = require('url');
const org = require('./../../.sfdx/org');
const { host } = url.parse(org.result.instanceUrl);
console.log(`${host.split('.')[0]}--namespace.visualforce.com`);
#!/bin/bash
while true;
do
CURRENT_TIME=$(date '+%Y-%m-%d %H:%M:%S')
SFDC_VF_DOMAIN=$(dig +short "$(node .github/sfdx_action/bin/get-visualforce-url.js)")
VF_RESULTS=$(grep -c "location.force" <<< "$SFDC_VF_DOMAIN")
echo -n "${CURRENT_TIME} Visualforce Domain: ${SFDC_VF_DOMAIN} "
if [ "$VF_RESULTS" = "0" ]
then
echo -n "${CURRENT_TIME} Visualforce Domain: ${SFDC_VF_DOMAIN} "
echo "...is ready for testing"
exit 0
else
echo "...not ready, retrying"
sleep 60
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment