You need to sign in to do that
Don't have an account?
DaveHagman
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
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.
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.
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 :
I'll go back and try it again. Thanks for the reply's. I'll let you know how it turns out.