1

Basically, I just want this trigger to update a custom number field (by incrementing it) whenever an email is sent or received from a case.

Here's the trigger:

trigger updateFieldUponEmail on EmailMessage (before insert) {
        Set<ID> caseIDs = new Set<ID>();

        for(EmailMessage msg : trigger.new){
            if(msg.parentID.getsObjectType() == Case.sObjectType)
               caseIDs.add(msg.parentID);
        }

        for(Case c: [SELECT timesUpdated__c FROM Case WHERE Id IN :CaseIDs]){
            c.timesUpdated__c++;
        }
}

Validates fine, deploys fine. It just doesn't actually increment that field. Not sure why. I tested by sending an email from a case. Am I testing wrong or is the code wrong?

6
  • 2
    where are you updating the cases? There is no DML statement.
    – Phil B
    Commented Oct 16, 2015 at 20:40
  • In the c.timesUpdated__c++; line
    – AKor
    Commented Oct 16, 2015 at 20:47
  • 1
    thats updating the record in memory but you need to commit it to the database with a statement like update caseList
    – Phil B
    Commented Oct 16, 2015 at 20:48
  • Sorry, I'm pretty new to APEX, how exactly would I go about doing so?
    – AKor
    Commented Oct 16, 2015 at 20:50
  • 1
    @Akor - Did you remove the asserts from the test class from my last answer to your previous question? If you did not remove them then the test would have failed.Just FYI as asserts are necessary to actually validate the trigger is doing what you want. Do not get into the habit of writing test to simply "cover lines"
    – Eric
    Commented Oct 16, 2015 at 21:42

2 Answers 2

6
trigger updateFieldUponEmail on EmailMessage (before insert) {
    Set<ID> caseIDs = new Set<ID>();

    for(EmailMessage msg : trigger.new){
        if(msg.parentID.getsObjectType() == Case.sObjectType)
           caseIDs.add(msg.parentID);
    }

    List<Case> casesToUpdate = [SELECT timesUpdated__c FROM Case WHERE Id IN :caseIDs];

    for(Case thisCase: casesToUpdate){
        thisCase.timesUpdated__c++;
    }

    update casesToUpdate;
}
1
  • 1
    N.B. assumes timesUpdated defaulted to zero in field definition metadata; also assumes that only one EmailMessage in batch per Case
    – cropredy
    Commented Oct 16, 2015 at 21:14
0

To address @crop1645's points and also to avoid an unnecessary query of Case when the email relates to other objects:

trigger updateFieldUponEmail on EmailMessage (before insert) {

    Map<Id, Integer> m = new Map<Id, Integer>();
    for(EmailMessage msg : trigger.new){
        if (msg.parentID.getsObjectType() == Case.sObjectType) {
            Integer n = m.get(msg.parentID);
            m.put(msg.parentID, (n != null ? n : 0) + 1);
        }
    }

    if (m.size() > 0) {
        List<Case> updates = [
                SELECT Id, timesUpdated__c
                FROM Case
                WHERE Id IN :m.keySet()
                ];
        for (Case c: updates){
            c.timesUpdated__c = (c.timesUpdated__c != null ? c.timesUpdated__c : 0)
                    + m.get(c.Id);  
        }
        update updates;
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .