function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mike SummittMike Summitt 

Function that returns field type "Email"

I'm trying to create a function that returns an email.  I want it to return a Salesforce field type Email, like you would put in an sObject, rather than a String.  The reason for this is that true email addresses are case sensitive before the @ and case insensitive after the @.  Here is the header of the function I'd like to change:

    webservice static ???.Email GetMainContactEmail(Id aid) {    <-- replace ??? with library name to qualify Email field type

Here is the test method that does not work because the function is using a String.

    public static testmethod void testGetMainContactEmailOnly() {
        Account testAccount = TestData.MakeAccount();
        Contact testContact = TestData.MakeContact(testAccount);
        System.assertEquals(testContact.Email, Utilities.GetMainContactEmail(testAccount.Id));
    }

The contact is created with a randomly generated mixed case email address consisting of 10 alphanumeric characters, '@', 10 alphanumeric characters, '.com'.  Here is the test result:

System.AssertException: Assertion Failed: Expected: 2BC8reZIC1@S1X422H1ou.com, Actual: 2bc8rezic1@s1x422h1ou.com

If anyone has a reliable alternative to trying to return the field type from the function, something that will still conform to the RFC requirements on case sensitivity, I would like to hear that also.

Thanks for your time and effort. 
Best Answer chosen by Mike Summitt
Mike SummittMike Summitt
Just saw this:

https://success.salesforce.com/ideaView?id=08730000000BpHyAAK

It looks like SF is out of compliance with the RFC, they know they're out of compliance, and they don't care.

All Answers

matt.ian.thomasmatt.ian.thomas
Can you post the code for your GetMainContactEmail() method? There is no email type in apex, it's just a String.
Mike SummittMike Summitt
Here is the code.  I guess the first thing I need to determine is does this function screw up the case of the data, or does SF screw up the case of the data when it stores the contact?  I thought that by returning type Email from this routine I could ensure the casing was preserved.

    webservice static String GetMainContactEmail(Id aid) {
        List<AccountContactRole> PrimariesByAccount = [SELECT ContactID
                                                       FROM AccountContactRole
                                                       WHERE AccountID = :aid
                                                       AND IsDeleted = false
                                                       AND IsPrimary = true];
        if (PrimariesByAccount != null && PrimariesByAccount.size() == 1) {
            Contact theContact = [SELECT Email FROM Contact where Id = : PrimariesByAccount[0].ContactID];
            return theContact.Email;
        }
        List<Contact> ContactsByAccount = [SELECT Id, Email
                                           FROM Contact
                                           WHERE AccountID = :aid];
        if (ContactsByAccount != null && ContactsByAccount.size() == 1) {
            return ContactsByAccount[0].Email;
        }
        return null;
    }
 
Mike SummittMike Summitt
Just saw this:

https://success.salesforce.com/ideaView?id=08730000000BpHyAAK

It looks like SF is out of compliance with the RFC, they know they're out of compliance, and they don't care.
This was selected as the best answer