[SalesForce] how to add email id as hyperlink in trigger error message
How can I add an email id as a hyperlink in a error message in a trigger?
Best Answer
You shouldn't.
Triggers are executing when your data is manipulated, from whatever context (UI or API). It would be a bad design to assume which interface is the source of your trigger execution and create user specific messages at that point.
That beeing said, that doesn't always applies to salesforce, as for standard pages you don't have the luxery of running custom controllers, and validation rules kinda skip the same principle.
ALSO: mailto hyperlinks are not perceived as user friendly by most users and often only work well with device installed email clients, and not with web email clients (I believe).
You can only view the email headers if they were saved when the email was received; doing so is the responsibility of your email handler (and will increase the storage space required for each email message).
Using Email-to-Case:
Configuration for whether email headers are saved or discarded is set for each routing address. To ensure that email headers are saved, go to Setup > Customize > Cases > Email-to-Case. Click "Edit" next to your routing address(es). Then, under "Email Settings," confirm that "Save Email Headers" is checked.
Using Apex Email Services
If you want to save the headers from an incoming message, your Apex Email Service is responsible for copying the headers from the incoming InboundEmail.headers and saving them to an EmailMessage.Headers in the database.
This is a 3rd-party app that enhances the functionality of SFDC's Email-to-Case. To save email headers, go to All Tabs > Email to Case Premium Settings > Inbound Configuration. Confirm that "Save Inbound Email Headers" is selected.
I think you are confused between the Messaging.sendEmail(messages) and the SObject EmailMessage used in the testclass.
EmailMessage is only used as a child of Case and is used to track incoming/outgoing emails on Cases
Messaging.sendEmail(..) will send an email but can't be tested using testmethods as testmethods won't send emails. The code will be 'invoked' but the email not sent (for obvious reasons - your testmethods could spam legitimate email addresses).
If you are concerned about whether you email message is being constructed correctly (hence verifying it with a testmethod), you'll want your prod code to break apart the construction of the email (perhaps by using a separate class) from the actual sending of the email. The testmethod can verify that the construction is accurate and you'll have to rely on faith that SFDC will actually send the email in PROD (which it will unless you've run out of daily apex emails -- something, BTW, you should reserve and test for in PROD code if the consequences of not sending the email are high. This is well-covered in the Apex Developer's Guide under outbound email.
UPDATE: With Enhanced Email, EmailMessage can be child of custom objects and most standard objects
Best Answer
You shouldn't.
Triggers are executing when your data is manipulated, from whatever context (UI or API). It would be a bad design to assume which interface is the source of your trigger execution and create user specific messages at that point.
That beeing said, that doesn't always applies to salesforce, as for standard pages you don't have the luxery of running custom controllers, and validation rules kinda skip the same principle.
ALSO: mailto hyperlinks are not perceived as user friendly by most users and often only work well with device installed email clients, and not with web email clients (I believe).