You need to sign in to do that
Don't have an account?
Eric 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; } }
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.
hope this will solve your issue.
Thanks
karthik
All Answers
Which line you were getting an error?
Thanks!!
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.
hope this will solve your issue.
Thanks
karthik