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
Eric FischlEric Fischl 

Trigger help, catch a potential null value

Hey all, I'm absolutely NOT a developer so this is probably straightforward ... looking for some help on trigger code. Basically I need to catch if an incoming email does NOT have a user record. This works as expected if the email has a user attached to it, posts correctly, but the else statement just never happens if the user does not exist, throws a list exception error. What am I doing wrong? Thanks!
 
public class EmailMessageCopyToCaseCommentsTrigger
{
    public static void copyEmailMessagesToCaseComments(List<EmailMessage> emails)
    {
        List<CaseComment> comments = new List<CaseComment>();
        for (EmailMessage email:emails)
        {
            Id caseId = email.ParentId;
            CaseComment comment = new CaseComment(ParentId=caseId);
            comment.IsPublished=true;
           Id UserDetails = [select id from user where email = :email.FromAddress].get(0).Id;
           String strId=Id.valueOf(UserDetails);
            if (!string.isEmpty(strId)){
                    comment.CreatedbyID = UserDetails;}
            else {comment.CreatedbyID = '0054x000005MWmAAAW';}
               
            String header = 'From: '+ email.FromName + ' <' + email.FromAddress + '>\n';
            header += 'To: '+ email.ToAddress + '\n';
            header += email.CcAddress!=null?'CC: '+ email.CcAddress + '\n\n':'\n';
            if (email.TextBody!=null) {
                comment.CommentBody = header + email.TextBody;
            } else if (email.HtmlBody!=null) {
                comment.CommentBody = header + email.HtmlBody.replaceAll('\\<.*?>','');
            }
            
            comments.add(comment);
        }
        
        if (!comments.isEmpty())
        {
            insert comments;
        }
    }

 
Best Answer chosen by Eric Fischl
karthikeyan perumalkarthikeyan perumal
Hello Eric, 

if the  SOQL for userdetails returning null, then next line will getting error. so bulkify the userdetails line and check the soql reurns value are not. in that else part you can define you  statement. i have modified your code. please check if this helps or not. 
 
public class EmailMessageCopyToCaseCommentsTrigger
{
    public static void copyEmailMessagesToCaseComments(List<EmailMessage> emails)
    {
        List<CaseComment> comments = new List<CaseComment>();
        for (EmailMessage email:emails)
        {
            Id caseId = email.ParentId;
            CaseComment comment = new CaseComment(ParentId=caseId);
            comment.IsPublished=true;
            List<user> UserDetails = [select id from user where email = :email.FromAddress].get(0).Id;
			if(UserDetails.size >0){
            String strId=Id.valueOf(UserDetails[0].Id);
            if (!string.isEmpty(strId)){
                    comment.CreatedbyID = UserDetails;}
            
			}else {comment.CreatedbyID = '0054x000005MWmAAAW';}
               
            String header = 'From: '+ email.FromName + ' <' + email.FromAddress + '>\n';
            header += 'To: '+ email.ToAddress + '\n';
            header += email.CcAddress!=null?'CC: '+ email.CcAddress + '\n\n':'\n';
            if (email.TextBody!=null) {
                comment.CommentBody = header + email.TextBody;
            } else if (email.HtmlBody!=null) {
                comment.CommentBody = header + email.HtmlBody.replaceAll('\\<.*?>','');
            }
            
            comments.add(comment);
        }
        
        if (!comments.isEmpty())
        {
            insert comments;
        }
    }

hope this will solve your issue.

Thanks
karthik
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Eric,

Which line you were getting an error?

Thanks!!
karthikeyan perumalkarthikeyan perumal
Hello Eric, 

if the  SOQL for userdetails returning null, then next line will getting error. so bulkify the userdetails line and check the soql reurns value are not. in that else part you can define you  statement. i have modified your code. please check if this helps or not. 
 
public class EmailMessageCopyToCaseCommentsTrigger
{
    public static void copyEmailMessagesToCaseComments(List<EmailMessage> emails)
    {
        List<CaseComment> comments = new List<CaseComment>();
        for (EmailMessage email:emails)
        {
            Id caseId = email.ParentId;
            CaseComment comment = new CaseComment(ParentId=caseId);
            comment.IsPublished=true;
            List<user> UserDetails = [select id from user where email = :email.FromAddress].get(0).Id;
			if(UserDetails.size >0){
            String strId=Id.valueOf(UserDetails[0].Id);
            if (!string.isEmpty(strId)){
                    comment.CreatedbyID = UserDetails;}
            
			}else {comment.CreatedbyID = '0054x000005MWmAAAW';}
               
            String header = 'From: '+ email.FromName + ' <' + email.FromAddress + '>\n';
            header += 'To: '+ email.ToAddress + '\n';
            header += email.CcAddress!=null?'CC: '+ email.CcAddress + '\n\n':'\n';
            if (email.TextBody!=null) {
                comment.CommentBody = header + email.TextBody;
            } else if (email.HtmlBody!=null) {
                comment.CommentBody = header + email.HtmlBody.replaceAll('\\<.*?>','');
            }
            
            comments.add(comment);
        }
        
        if (!comments.isEmpty())
        {
            insert comments;
        }
    }

hope this will solve your issue.

Thanks
karthik
 
This was selected as the best answer
Eric FischlEric Fischl
Thanks for everyone's help!