Repository navigation
Developer Documentation
This page sets up an org from the source code, so you can change and test the package. To install the released package instead, see Installation.
-
Salesforce CLI (
sf). - Git, or GitHub Desktop.
- Optional: VS Code with the Salesforce Extension Pack.
- An org to work in. Use one of these:
- Scratch org (recommended). You need a Dev Hub. A free Developer Edition org can act as one: Setup > Dev Hub > Enable Dev Hub.
- Developer Edition org, Trailhead Playground or sandbox. Step 2 covers turning on Digital Experiences.
git clone https://github.com/SFDO-Community/UnsubscribeLink
cd UnsubscribeLink
For a scratch org:
sf org create scratch -f config/project-scratch-def.json -a unsub-dev -v my-devhub --no-namespace -y 30 -w 15
Replace my-devhub with the alias of your Dev Hub. If the CLI isn't logged in to your Dev Hub yet, log in first:
sf org login web --alias my-devhub
-
--no-namespace.sfdx-project.jsondeclares the package namespaceUnsubscribeLnk, but that namespace isn't linked to your Dev Hub, so scratch org creation fails without this flag. - Language. If your Dev Hub's default language isn't English, the scratch org inherits that language, and the deploy in step 3 fails. Use a Dev Hub with English as its default language.
For a Developer Edition org, Trailhead Playground or sandbox, log in:
sf org login web --alias unsub-dev
For a sandbox, add --instance-url https://test.salesforce.com to the login command.
Then turn on Digital Experiences in the org: Setup > Digital Experiences > Settings > Enable Digital Experiences > Save. Once it's on, it can't be turned off again. Scratch orgs don't need this, because the scratch org definition already turns it on.
Every command on this page names the org with -o, so you don't need a default org. If you use the VS Code extension, make this org the default, because the extension's commands use it:
sf config set target-org unsub-dev
sf project deploy start -o unsub-dev
sf org assign permset -n Unsubscribe_Link -o unsub-dev
The unsubscribe page runs on an Experience Cloud site. The source code doesn't include the site, so you create it yourself.
sf community create -n Unsubscribe -t "Build Your Own" -p Unsubscribe -o unsub-dev
The site name and the URL prefix must both be Unsubscribe. The links are built from that site name.
Build Your Own is the Aura template. Don't use Build Your Own (LWR), because the page and the steps below are for Aura.
The command prints a Job Id. Provisioning can take a few minutes. Wait until this query shows Complete:
sf data query -o unsub-dev -q "SELECT Status FROM BackgroundOperation WHERE Id = '<Job Id>'"
The site's guest profile is called Unsubscribe Profile. Guest users can only run flows they are granted one by one. The guest profile doesn't appear in Setup > Profiles, so open it from the site instead:
- Open the site in Experience Builder: Setup > Digital Experiences > All Sites > Builder (next to Unsubscribe).
- Go to Settings (gear icon) > General, and click the
Unsubscribe Profilelink under Guest User Profile. - On the profile page, find Enabled Flow Access and click Edit.
- Add these four flows and save:
- Unsubscribe Contacts
- Unsubscribe Leads
- Unsubscribe Link No RecordId
- Unsubscribe Link Quick
In Experience Builder:
-
Add the page. Open the Pages menu at the top left, then New Page > Standard Page, and pick
Unsubscribefrom the list, which already contains the flow. Set both the name and the URL tounsubscribe. - Allow guests. Go to Settings > General and tick "Guest users can see and interact with the site without logging in".
- Publish. Click Publish at the top right, then Publish again in the dialog.
- Activate. Click the Builder icon at the top left, then Administration > Settings, and click Activate. Confirm the browser dialog with OK. A site that is published but not active still sends guests to the login page.
To check, open https://<your site domain>/Unsubscribe/s/unsubscribe in a private browser window. It should load without asking you to log in.
Open Setup > Flows, open Unsubscribe Link Setup, and click Run. The flow:
- creates the Unsubscribe Link Setup record,
- can create an org-wide email address for confirmation emails,
- runs the batches that give every existing Contact and Lead a Public Id and an unsubscribe link.
If you chose confirmation emails, verify the org-wide email address by clicking the link in the email Salesforce sends.
The setup flow's last screen says two record-triggered flows will create Public Ids for new records. They don't do that until you activate them in step 8.
Activate the flows Contact_After_Create_Update_Set_Public_Id and Lead_After_Create_Update_Set_Public_Id. Until you do, new Contacts and Leads get no unsubscribe link.
To test, create a Contact with an email address and open the link in its Unsubscribe Url Plain Text field in a private browser window.
sf apex run test -o unsub-dev --test-level RunLocalTests --wait 20 --result-format human
Pull requests run the same Apex tests in a fresh scratch org through the feature test workflow (.github/workflows/feature.yml), and need 75% code coverage to pass.