19

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: foe@testorg.com (Foe Test)
When saving the record via SF UI, the result is: foe@testorg.com (foe test)
When saving the record using Apex, the result is: foe@testorg.com

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 = 'testbill0@billing.com (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);
  • 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 '14 at 23:12
  • very interesting -- in spring 14 detail page, shows as foe@testorg.com but in listview shows as foe@testorg.com (foe test). Value in db is foe@testorg.com (foe test). – cropredy Jun 23 '14 at 23:35
  • That is correct: the list view shows field as "foe@testorg.com (foe test)"; the value on detail page view mode is shown as "foe@testorg.com"; the value in edit mode is shown as "foe@testorg.com (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. – RadDeveloper Jun 23 '14 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 '14 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. – RadDeveloper Jun 24 '14 at 18:24
-1

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 john.doe@example.com(comment) are both equivalent to john.doe@example.com.

Example email address:

abcdefg.hijklmnopqrstuvwxyz!#$%&'*/=?^_+-`{|}~0123456789@acme-inc.com

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.