You need to sign in to do that
Don't have an account?
Mike 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.
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.
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
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;
}
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.