25

When an email contains a label enclosed in parentheses, the value can be saved as typed in via SF UI. (Note: the email value "as is" is only seen when in Edit mode.) However, when the record is saved using Apex or Query Editor on Developer Console, the content in parentheses is lost.

For example, a user specifies an email as: [email protected] (Foe Test)
When saving the record via SF UI, the result is: [email protected] (foe test)
When saving the record using Apex, the result is: [email protected]

Why is there inconsistency in saving an email value. How can we save an email containing parentheses using Apex?

Account acc = new Account(name = 'Test Email');
insert acc;

String email = '[email protected] (any label)';
Contact c = new Contact (lastName = 'Test', accountId = acc.id, email = email);
insert c;

Contact cDb = [
    SELECT
        id,
        accountId,
        name,
        email
    FROM Contact
    WHERE id = :c.id
][0];

System.assertEquals(c.email, cDb.email);
6
  • can you paste a screenshot/ when i tried to save an email address in the contact object through UI, I see just the email and not the (Foe Test) with the email.
    – Rao
    Jun 23, 2014 at 23:12
  • very interesting -- in spring 14 detail page, shows as [email protected] but in listview shows as [email protected] (foe test). Value in db is [email protected] (foe test).
    – cropredy
    Jun 23, 2014 at 23:35
  • That is correct: the list view shows field as "[email protected] (foe test)"; the value on detail page view mode is shown as "[email protected]"; the value in edit mode is shown as "[email protected] (foe test)". So there is inconsistency in how it is displayed in SF UI. However, the main issue is that the value is not saved as specified in DB when using DML. This creates problems when trying to synchronize tables/records on email. Jun 23, 2014 at 23:51
  • Ultimately, those parens will need to be escaped. This is a result of last year's updates that changed the way HTML was escaped/unescaped in VF (can't recall the exact details, but I believe it eliminated the need) and handling of the same in APEX. So unless they're explicitly escaped, they're ignored in certain types of fields such as an email field.
    – crmprogdev
    Jun 24, 2014 at 15:30
  • The parentheses were escaped in many of my attempts to solve this issue. None of them worked. Moreover, parentheses are not considered as special characters (so far) since such email can be entered via SF UI but when using Apex, the content inside parentheses is just ignored, no error is issued and the rest of the email is inserted. Jun 24, 2014 at 18:24

2 Answers 2

1

The inconsistency could be a bug that has been fixed. Cannot replicate it now. The result is "[email protected]" in both SF UI and Apex.

0

Comments - denoted by matched parentheses - are allowed in the domain part as well as in the local part. Comments in an email address are ignored when sending email; for example, john.doe@(comment)example.com, and [email protected](comment) are both equivalent to [email protected].

Example email address:

abcdefg.hijklmnopqrstuvwxyz!#$%&'*/=?^_+-`{|}[email protected]

https://help.salesforce.com/articleView?id=000001145&language=en_US&type=1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .