You need to sign in to do that
Don't have an account?
Mike Reynolds 6
Using trigger to update case when first emailMessage is received
Hey,
I'm trying to update a case with some details of the related email message. I need the case to be updated before Assignment rules are run. I would also like to limit the trigger to only fire when this is the first email associated to the case. I've been able to get it this far, but it's not working and I have no idea where I've gone wrong. Any help is greatly appreciated.
Once this is working I'll move much of the work into a helper class and refactor the trigger into the existing case trigger.
I'm trying to update a case with some details of the related email message. I need the case to be updated before Assignment rules are run. I would also like to limit the trigger to only fire when this is the first email associated to the case. I've been able to get it this far, but it's not working and I have no idea where I've gone wrong. Any help is greatly appreciated.
Once this is working I'll move much of the work into a helper class and refactor the trigger into the existing case trigger.
trigger EmailToCaseTrigger on Case(after insert) { system.debug('Here in Trigger'); List<case> cases=new List<Case>(); for(Case c:Trigger.new){ List<EmailMessage> em=[select ToAddress,FromAddress,CcAddress,FromName from EmailMessage where ParentId=:c.Id and Incoming=true and Parent.Number_of_Messages_Received__c=0]; if(em!=null && em.size()>0){ c.Email_To_Address__c=em[0].ToAddress; c.From_Address__c=em[0].FromAddress; c.Email_CC_Address__c=em[0].CcAddress; c.Email_From_Name__c=em[0].FromName; cases.add(c); } } if(cases.size()>0) update cases; }
Hi Mike,
I see that the trigger you have developed does not follow Salesforce best practices. You have SOQL queries inside the for loop which is a bad practice. Please review the below link for the best practices.
https://developer.salesforce.com/page/Apex_Code_Best_Practices
Try this piece of code for now
Let me know in case you face any issues.
Thanks
Vivian
All Answers
Hi Mike,
I see that the trigger you have developed does not follow Salesforce best practices. You have SOQL queries inside the for loop which is a bad practice. Please review the below link for the best practices.
https://developer.salesforce.com/page/Apex_Code_Best_Practices
Try this piece of code for now
Let me know in case you face any issues.
Thanks
Vivian