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
DaveHagmanDaveHagman 

Casting email data type to String

Hi All,

 

I am trying to use the email field under the User object to send an outgoing single email message. The problem I am having is that when I query for the email address in the User table the email address is of type Email, not String so when I use the setToAddresses method in the SingleEmailMessage object it doesn't work because it expects an array of String. How can I cast the User.Email field from type email to String so I can use it in the setToAddresses method?

 

Example Code:

 

 

User assignedUser = [select u.id,
			u.email   // This is of type Email, not String
			from User u
			where u.id = :c.OwnerId
			limit 1];						  
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new String[] { assignedUser.Email });   // This is how I want to use it This won't work because Email object cannot be casted to a String

 

 

David81David81

Not sure why you are seeing an issue. There really is no "Email" datatype. That's only in the UI.  In Apex, emails are strings.  Are you getting an actual error message? If so, what is it?

 

That code works fine for me.

DaveHagmanDaveHagman

I believe it said "Not a valid data type as parameter for String[] constructor" or something similar... Sorry I am not at my workstation at the moment so I can't get the exact compiler error message.

David81David81

That an odd one. Are you sure the error was coming from that line of code? It looks like this is a piece of something larger.

 

As an example, this is the output when I run a quick test through the Execute Anonymous area in Eclipse :

 

 

Execute Anonymous: User u = [SELECT u.Id,u.Email from User u WHERE u.email!=null LIMIT 1];
Execute Anonymous: Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
Execute Anonymous: email.setToAddresses(new String[]{u.Email});
Execute Anonymous: system.debug(email);
17:36:38.781|EXECUTION_STARTED
17:36:38.781|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
17:36:38.782|SOQL_EXECUTE_BEGIN|[1]|Aggregations:0|SELECT u.Id,u.Email from User u WHERE u.email!=null LIMIT 1
17:36:38.790|SOQL_EXECUTE_END|[1]|Rows:1
17:36:38.790|METHOD_ENTRY|[3]|Messaging.SingleEmailMessage.setToAddresses(LIST<String>)
17:36:38.791|METHOD_EXIT|[3]|Messaging.SingleEmailMessage.setToAddresses(LIST<String>)
17:36:38.791|METHOD_ENTRY|[4]|System.debug(ANY)
17:36:38.791|USER_DEBUG|[4]|DEBUG|Messaging.SingleEmailMessage[getBccAddresses=null;getCcAddresses=null;getCharset=null;getDocumentAttachments=null;getFileAttachments=null;getHtmlBody=null;getInReplyTo=null;getOrgWideEmailAddressId=null;getPlainTextBody=null;getReferences=null;getTargetObjectId=null;getTemplateId=null;getToAddresses=(******@******.com);getWhatId=null;isUserMail=false;]
17:36:38.791|METHOD_EXIT|[4]|System.debug(ANY)

 

 

DaveHagmanDaveHagman

I'll go back and try it again. Thanks for the reply's. I'll let you know how it turns out.