I have a customer that wants to have a field on the case updated when a user sends an email from a case. For simplicity sake, let's just say we need to put some string into a custom field each time an email is sent from a case.
The way I think I would accomplish this would be to write a trigger on the EmailMessage object checking that the email was not inbound and update the field accordingly. Seems pretty straight forward and simple. Seems like something like this should work
trigger exampleTrigger on EmailMessage (after insert) {
set<Id> caseIds = new set<Id>();
map<Id,string> case2CustomValueMap = new map<Id,sting>();
for(EmailMessage message : trigger.new){
if(message.Incoming == false){
string myCustomValue;
//Some sort of logic that gets the value for this
caseIds.add(message.ParentId);
case2CustomValueMap.put(message.ParentId, myCustomValue)
}
}
list<Case> casesToUpdate = [Select Id, Number, Custom_Field__c From Case Where Id in: caseIds];
for(Case c : casesToUpdate){
c.Custom_Field__c = case2CustomValueMap.get(c.Id);
}
update casesToUpdate;
}
My question revolves around the EmailMessage object itself. Is this exclusive to cases. In the documentation it makes it seem as though it is only used for cases as the ParentId refers to the Case its related to.
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_emailmessage.htm
So basically I want to know if writing a trigger on the EmailMessage object is:
- The only way to accomplish a field update on the case object whenever an email is sent from that case.
- The most efficient way of accomplishing it
Any clarification or other possibilities would be appreciated.