This article describes about retrieving information like "from email address" and "from name" (first name and last name). Is there a possibility that FromName field can be null?

I am getting null pointer exception with this line in my trigger on EmailMessage. I am trying to replace a string in email template with the name of email's sender.

htmlvalue = htmlvalue.replace('{!Case.Contact}', msg.FromName);


2 Answers 2


Have you checked that:

Htmlvalue isn't null, perhaps you received an email, only containing Text (so no HTML), and the value is null?

  • Htmlvalue is the HTML content of email template in my dev org. I am trying to replace '{!Case.Contact}' by name of the person who sent email to the service address defined under email2case settings in my dev org. My question is whether 'fromname' of email sender could be null. 'Fromname' is one of the fields of EmailMessage in Salesforce. Oct 19, 2014 at 15:18

It is possible for the fromname to be null. I would recommend you test for that and if it is null consider extracting the first part of the email (before the @), and use that instead.

Alter your current code like this:

String fName;
if(msg.FromName != null){
      fName= String.valueOf(msg.FromAddress).split('@')[0];
 htmlvalue = htmlvalue.replace('{!Case.Contact}', fName);

This should work, might need a little tweaking.

  • Ok. How do I test for null fromname? When I send email using outlook or gmail it takes the display name from my account. I tried setting the display name as null in the code using Sendemail function. But it takes the name of the user who is running the code in execute anonymous. So, am not able to set the fromname as null. Any suggestions? Thanks. Oct 23, 2014 at 17:58
  • Updated with some code to help out. Could you explain more about what you are trying to accomplish? From your other comment, it appears you are trying to parse an email template and modify it with a trigger. Maybe if you describe the full business process we can come up with a better solution.
    – JimRae
    Oct 23, 2014 at 21:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .