1

I have a email to case. Once an email is received from a service address , a case gets created and the issue is that trigger is not getting fired. I cross checked with a workflow rule, the workflow rule is able to work properly when an email is received. I have created a workflow on email message Object. But trigger is not working

trigger EmailResponse on EmailMessage (after insert) {
    //Boolean custResp;
    Datetime theDate;
    set<Id> caseIds = new set<Id>();
    map<Id,Datetime> case2CustomValueMap = new map<Id,Datetime>();
    for(EmailMessage message : trigger.new){
        if(message.Incoming == true){
            system.debug('.....EMAIL RECEIVED');
            //Some sort of logic that gets the value for this
            theDate = message.MessageDate;
            system.debug('.....theDate '+theDate);
        }
        /*
        else 
            custResp = False;

        if(Trigger.isUpdate){
            system.debug('Update happened');
            custResp = FALSE;
        }
        */

        caseIds.add(message.ParentId);
        case2CustomValueMap.put(message.ParentId, theDate);
        //case2CustomValueMap2.put(message.ParentId, custNoResp);        
    }

    list<Case> casesToUpdate = [Select Id, LastModifiedDate, CreatedDate, Last_Date_time_when_email_occurred__c From Case Where Id in: caseIds];
    for(Case c : casesToUpdate){
        //c.RESP__c = case2CustomValueMap.get(c.Id);
        c.Last_Date_time_when_email_occurred__c = case2CustomValueMap.get(c.Id);
        system.debug('.....Last Date time ' +c.Last_Date_time_when_email_occurred__c);
        //if(Trigger.oldMap.get(c.Id).LastModifiedDate != c.LastModifiedDate)
        // c.Customer_Response__c = FALSE;
    }
    update casesToUpdate;
}

I'm unable to capture it even in Debug Logs. Trigger is fine. But trigger is not getting fired . Please help.

  • is this really the code? It won't compile as custResp is commented out – cropredy Sep 3 '14 at 23:13
  • CustResp is not am using, am using a DATETIME field to get updated when am email occurs. Please check again – AjaySFDC Sep 4 '14 at 8:52
  • oops, my bad - you had commented this code out -- for those of us in SFSE, it is helpful to remove commented code before posting – cropredy Sep 4 '14 at 17:29
1

If you're certain that the trigger is not getting fired (as opposed to being fired & not performing the expected functionality) then the most obvious thing to check would be is if the trigger is active?

If you look at the trigger in the Force.com setup menu you will see a 'Status' field, what value is it displaying?

You can also check this is the dev. console & force.com IDE, but the UI is easiest.

Edit: OK, as the trigger is firing, here's why I think it's not working.

You definte theDate as null here:

Datetime theDate;

Then you set it, but only if message.Incoming is TRUE:

if(message.Incoming == true){
        system.debug('.....EMAIL RECEIVED');
        //Some sort of logic that gets the value for this
        theDate = message.MessageDate;
        system.debug('.....theDate '+theDate);
    }

If message.Incoming is false, then you're putting a null value against the case Id in case2CustomValueMap:

case2CustomValueMap.put(message.ParentId, theDate);

So when you update the Cases, you're potentially putting null values in Case.Last_Date_time_when_email_occurred__c:

c.Last_Date_time_when_email_occurred__c = case2CustomValueMap.get(c.Id);

This give a try, I've cut it down a bit to make it simpler:

trigger EmailResponse on EmailMessage (after insert) {

    Map<Id,Datetime> case2CustomValueMap = new Map<Id,Datetime>();

    for (EmailMessage message : trigger.new) {

        if (message.Incoming) {

            case2CustomValueMap.put(message.ParentId, message.MessageDate);
        }
    }

    Map<Id, Case> casesToUpdate = new Map<Id, Case>();

    for(Case c : [  Select Id, LastModifiedDate, CreatedDate, 
                        Last_Date_time_when_email_occurred__c 
                    From Case 
                    Where Id in: case2CustomValueMap.keySet()   ]) {

        if (case2CustomValueMap.get(c.Id) != null && 
            case2CustomValueMap.get(c.Id) != c.Last_Date_time_when_email_occurred__c) {

            c.Last_Date_time_when_email_occurred__c = case2CustomValueMap.get(c.Id);

            casesToUpdate.put(c.Id, c);
        }
    }

    if (!casesToUpdate.values().isEmpty()) {

        update casesToUpdate.values();
    }
}
  • Yes , I have done that basic step .. It is active, and also it has 91 % code coverage. I dont know y its not working :( – AjaySFDC Sep 4 '14 at 8:54
  • OK I've had another look and edited my post, take a look and let me know if that's what the problem was. – Davin Casey Sep 5 '14 at 9:33

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.