0

Of course people will suggest to use EmailMessage trigger but if I update case from there then the problem is in case auto response rule as our requirement is mainly to fire the autoreply after the case trigger insert the case original recipient email.

Code

    trigger CaseTrigger on Case (after  insert) {
    
        List<Case> caselist = new List<Case>();
    
        EmailMessage emailMsg= [SELECT ToAddress  FROM EmailMessage 
                                 WHERE ParentId IN : trigger.newMap.keyset() ];
                               //this says no Sobject error
    
        for(Case caseData : trigger.new)
        {
            
            Case origcase = new Case(Id=caseData.Id, Original_Recipient__c = 'kamran.jabbar@test.com'); //This will work.
    
            Case origcase = new Case(Id=caseData.Id, Original_Recipient__c = emailMsg.ToAddress); //This is not working.
            
            caselist.add(origcase);
        }
    
        update caselist;
        
    }

I want the email message ToAddress field value dynamically to set in case field.

Is there some trick to fix this problem? If it is not possible then we need to change a big infra for that, please help. thank you.

4
  • 2
    In E2C, the Case object is inserted before the EmailMessage object; hence your SOQL will return no rows
    – cropredy
    Nov 5, 2020 at 17:06
  • Yes I can understand but we have to set case auto response rule based on that emailmessage to address field, is it possible via some other way? Nov 6, 2020 at 7:09
  • If I set the value in email message trigger to case then that value is not available in case auto response conditions as per the execution order. Nov 6, 2020 at 7:10
  • you're going to need to use process automation (WFR, PB, Flow) to replace your Case auto-response rules - then you have more control over when they execute
    – cropredy
    Nov 6, 2020 at 19:44

1 Answer 1

-1

I know you probably have already found a workaround by now, but I think the primary issue if order of execution on insert (and what fields are set at each time).

From my understanding, when the email comes in, it does not have a relatedTo just yet because the Case has not been created. So then the Case gets created . Then once the Case finally has an Id (after insert) the EmailMessage is then updated with the RelatedTo (the Case Id).

I would in this case not rely on the auto-response rule when the 'Email Message: To Address' is important to you. I would potentially try using other specific criteria on the auto-response rule, or do something in other automation that can trigger on edit of that new Case/EmailMessage.

Found a similar question here: https://developer.salesforce.com/forums/?id=906F00000008z2HIAQ

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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