14

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:

  1. The only way to accomplish a field update on the case object whenever an email is sent from that case.
  2. The most efficient way of accomplishing it

Any clarification or other possibilities would be appreciated.

7

The EmailMessage object ParentId is exclusive to Cases (at least based on current Salesforce schema information). You can always branch your code to inspect the ParentId with the Case object prefix value (500). To my knowledge, your solution of adding a trigger on EmailMessage is the only viable solution.

8

Actually, you can do this using a workflow (although it depends on exactly what you are doing).

My goal was to have a time/date field that got updated when an a case received an email, as a way to keep cases current in the list (since normally the date doesn't change).

You can also take advantage of this to re-open cases automatically when an email is received.

  1. Create a new timestamp field called LastRecievedEmail on the case object
  2. Setup -> Workflow -> Workflow rules -> New Workflow - evaluate when created. On the Email Message Object

  3. Rule Criteria: Case Number not equal to null (empty field) AND isIncoming equal True

  4. Field Update -> Update LastRecievedEmail to the value of NOW()

1

Yes it is the best way to achieve this functionality and you may also consider to filter only outbound messages using the boolean field Incoming

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.