Sub topic 8
Email, Phone and URL Fields
This chapter assumes you know what an object, a record and a field are. It also assumes you can create a custom field in Object Manager and read an API name. Email, Phone and URL are three specialty text types. They store plain strings, but Salesforce adds validation, formatting or clickable behavior on top. Knowing exactly what each one adds, and what it does not, prevents most of the mistakes people make with them.
What these three types have in common
Each of the three types stores a string. None of them is a different kind of data underneath. The type tells Salesforce how to display the value and what to check when someone saves it.
| Type | Maximum length | Validation on save | Behavior in the UI |
|---|---|---|---|
| 80 characters | Checks that the value looks like an email address | Shown as a `mailto:` link | |
| Phone | 40 characters | None on the content | Click-to-dial where a softphone or mobile app supports it |
| URL | 255 characters | Must start with a protocol, such as `http://` or `https://` | Shown as a clickable link |
Because the length limits are fixed, you cannot raise them. If a value may be longer, you need a plain Text field or a Long Text Area and you give up the specialized behavior.
Email fields
An Email field accepts one address. The platform checks the basic shape of the value: some text, an @, and a domain. It does not check that the mailbox exists. A value such as [email protected] passes.
Useful details:
- Unique and External ID. You can mark a custom Email field as unique or as an external ID. Uniqueness is case-insensitive by default, so
[email protected]and[email protected]count as the same value unless you choose a case-sensitive option. - One address per field. A semicolon-separated list fails validation. If a record needs several addresses, create several Email fields or use a related object, such as a child record for each address.
- Standard fields.
Contact.EmailandLead.Emailare standard Email fields. Salesforce uses them for email sending, duplicate matching and Email-to-Case routing. A custom Email field does not get that special treatment. If you need a second address that behaves like the standard one in email sends, check the feature documentation before assuming it will. - Bounce handling. Bounce management is a separate org setting. It marks the standard email field on a record when messages bounce. It does not apply to your custom Email fields.
An Email field also appears in Apex as a String. Its describe type is EMAIL.
Schema.DescribeFieldResult f = Contact.Email.getDescribe();
System.debug(f.getType()); // EMAIL
System.debug(f.getLength()); // 80Phone fields
A Phone field stores text up to 40 characters. Salesforce does not validate the content. The value call after lunch saves without an error.
That lack of validation is the main gotcha. If you need clean data, you have three options:
- Add a validation rule that tests the format with
REGEX. - Use a Flow or trigger to normalize the value, for example to E.164 format (
+14155550123). - Accept free-form input and normalize later, when you integrate with a dialer or messaging system.
A validation rule for a ten-digit US number, with optional punctuation, can look like this:
AND(
NOT(ISBLANK(Phone)),
NOT(REGEX(Phone, "^\\(?[0-9]{3}\\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$"))
)The rule fires, and blocks the save, when the phone value is filled in but does not match the pattern. Rules like this are strict. If your users work in several countries, a US-only pattern will reject valid numbers, so test the rule against real data before you activate it.
Some orgs turn on automatic phone formatting for standard fields. That setting changes how the value appears, and it only applies to certain standard fields and locales. Do not rely on it to clean data in custom Phone fields.
Phone fields also have a practical search effect. Salesforce search matches phone numbers more loosely than other text, so searching for the digits usually finds a number even if it was saved with spaces or dashes. Filters in SOQL and reports do not behave that way. They compare the stored string exactly.
SELECT Id, Name FROM Contact WHERE Phone = '(415) 555-0123'This query does not return a contact stored as 415-555-0123. If your integrations filter on phone numbers, store them in one consistent format.
URL fields
A URL field holds up to 255 characters. Salesforce requires a protocol, so www.example.com fails and https://www.example.com passes. In the UI, the value shows as a link that opens in a new tab or window.
Points to watch:
- Length. Long tracking URLs with many query parameters can exceed 255 characters. The save then fails. Use a Long Text Area if you must keep the whole string, or store a shortened URL.
- Display text. A URL field shows the raw address. If you want a readable label such as "Open in billing system", use a formula field that returns text with the
HYPERLINKfunction. - Security. Treat stored URLs as untrusted input when you build links in code. Do not follow a stored URL on the server without checking the host. Do not render it in custom components without encoding.
A formula field with a Text return type can build a labeled link from an ID:
HYPERLINK("https://billing.example.com/accounts/" & Billing_Id__c, "Open in billing system")This chapter's formula example is a simple one. The Formula chapter covers return types, functions and limits in full.
Metadata view
In metadata, the three types are Email, Phone and Url. Only Email and Url declare no length, because their limits are built in.
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Billing_Email__c</fullName>
<label>Billing Email</label>
<type>Email</type>
<required>false</required>
<unique>false</unique>
</CustomField>Choosing between these types and plain Text
The specialized types are worth using when you want the built-in behavior. Use plain Text when you do not.
| Requirement | Better choice |
|---|---|
| One validated email address | |
| Several addresses in one field | Several Email fields, or a related object |
| Phone number with click-to-dial | Phone |
| Strictly validated phone format | Phone plus a validation rule |
| Web address under 255 characters | URL |
| Web address that may be longer | Long Text Area |
| Link with a readable label | Formula with `HYPERLINK` |
A common mistake is to use a Text field for a website because it seems simpler. You then lose the link, the protocol check and the signal to other admins about what the field holds. Another mistake is to trust the Email type to prove an address works. It checks format only.
Changing these types later
You can usually switch between Text and Email, Phone or URL. Salesforce checks the existing values, though. If a Text field holds values that are not valid emails, the conversion to Email fails or the invalid data causes problems. Clean the data first. The Changing a Field Type chapter covers the rules in detail.
Exercise
A company stores customer websites in a URL field. A sales rep tries to save www.acme.com and gets an error, and another rep cannot save a long campaign link of 400 characters. Explain both failures and give a fix for each.
Show answer
The first value fails because a URL field requires a protocol, so the rep must enter https://www.acme.com. The second value fails because a URL field holds at most 255 characters. The fix is to store the long link in a Long Text Area, or to store a shortened version of it in the URL field.
Exercise
You need to guarantee that a custom Phone field only contains ten-digit US numbers. Which feature enforces this, and why does the Phone type not do it on its own?
Show answer
A validation rule that uses REGEX enforces the format. The Phone type stores free-form text up to 40 characters and does not check the content, so any string can be saved unless a rule blocks it.