Check that an Account has at least one Contact
Create a Record Health Check that counts the Contacts related to an Account and passes when there is
at least one. When there are none, the card links to the Account’s Contacts related list. The example
uses the Verify with a query Evaluation Type with a COUNT() query.
Why a query fits
Section titled “Why a query fits”Contacts are separate records related to the Account, so a formula on the Account cannot count them. One SOQL query can. Use a Check rather than a Validation Rule because an Account without Contacts is a gap to fix before a handoff, not a reason to block every Account update.
Before you start
Section titled “Before you start”- Install Record Health Check, and assign yourself Record Health Check Admin and the Salesforce Customize Application permission.
- Confirm that the people who will use the card can read Contacts. The count includes only Contacts the running user can see.
Step 1: Create or choose the Check Set
Section titled “Step 1: Create or choose the Check Set”You can add the Check to an Account Check Set you already have. To create one, open Setup → Custom Metadata Types, select Manage Records next to Record Health Check Set, select New, enter these values, and save. Leave other fields at their defaults.
| Field | Value |
|---|---|
| Label | Account Related Record Review |
| Object | Account |
| Record Health Check Set Name | Account_ |
| Active | Cleared until validation succeeds |
| Card Title | Account Related Record Review |
| Card Subtitle | Confirm related records are ready for handoff. |
| When Checks Run | When the user clicks Run |
| Found/Expected Display | Show on demand |
Step 2: Create the Check
Section titled “Step 2: Create the Check”Open Setup → Custom Metadata Types, select Manage Records next to Record Health Check, select New, enter these values, and save.
| Field | Value |
|---|---|
| Label | Has At Least One Contact |
| Check Set | Account Related Record Review |
| Evaluation Type | Verify with a query |
| Record Health Check Name | Has_ |
| Evaluation Order | 10 |
| Active | Cleared until validation succeeds |
| Source Query | SELECT COUNT() FROM Contact WHERE AccountId = {!record. |
| Source Query Field | Leave blank |
| How To Read Query Results | One row or aggregate |
| Comparison Operator | Greater than |
| Expected Value Comes From | Fixed value |
| Expected Value (Fixed) | 0 |
| Check Title | Account has at least one Contact |
| Check Description | Passes when the Account has one or more Contacts that you can see. |
| Message When Failed | {!record. |
| Fix Message | Add a Contact for this Account, then run the Check again. |
| Message When Unable To Evaluate | Unable to count Contacts. |
| Failure Severity | Warning |
| Action Label | Review Contacts |
| Action URL | /lightning/r/Account/{!record. |
How the values work:
SELECT COUNT()returns one number, the count of matching Contacts. Leave Source Query Field blank, because a bareCOUNT()returns the number directly.{!record.Id}inserts the open Account’s ID. Do not put quotation marks around it.- Greater than with an expected value of
0passes when the count is 1 or more. - A
COUNT()query always returns a number, even 0, so If Query Finds No Records does not apply.
Step 3: Validate and activate
Section titled “Step 3: Validate and activate”- Run the Validate Record Health Check Configuration Flow action, as described in Validate configuration before activation, and correct every error.
- Activate the Check, then the Check Set.
- Add the Record Health Check component to the Account record page, select Account Related Record Review (Account_Related_Record_Review), and activate the page.
- Assign Record Health Check Card User to the users who test the card.
Step 4: Test the Check
Section titled “Step 4: Test the Check”Select Run on the card for each case.
| Contacts the user can see | Result on the card | Found | Expected |
|---|---|---|---|
| 0 | Warning | 0 | more than 0 |
| 1 | Pass | 1 | more than 0 |
| 5 | Pass | 5 | more than 0 |
Also run the card as a user who cannot see any of the Account’s Contacts. The Check shows Warning for that user, because it counts only visible Contacts.
What users see
Section titled “What users see”A failed row shows Warning, the message, the fix message, a Review Contacts link, and Found and Expected values. Expected combines the operator and the value, so it reads “more than 0”.
Resolve problems
Section titled “Resolve problems”| What you see | What to check |
|---|---|
| Every Account fails | Comparison Operator is Greater than and the expected value is 0. |
| The count is lower than expected | The running user’s access to the Account’s Contacts. |
| Unable to Check | The user can read Contacts, and Source Query matches the table exactly. |
| A validation error on Source Query Field | Leave it blank for a bare COUNT() query. |