function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Allen2Allen2 

Case assignment rules are not firing based on emailMessage.toaddress

I've written the below code to update the field on case object when we're getting email to case. But when I tried to write logic to fire case assignment rule based on whatever value we're having in emailMessae.toAddress, it's not working at all.
Public class emailMessageHelper {
    Public static void updateCase (list<EmailMessage> newEmailMessageList, map<Id, EmailMessage> oldMap){
        Set <Id> caseIdSet = new Set <Id>();
        list <Case> caseList = new List <Case>();
        Map<Id, Case> caseMap = new Map<Id, Case>();
        list<Case> newCaseList = new list<Case>();
        
        for (EmailMessage messageObj: newEmailMessageList)
        {
            caseIdSet.add(messageObj.ParentId);
        }
        
        if(caseIdSet.size()>0)
        {
            caseList=[Select Id, OwnerId, Owner.Email, Status, status_Alert__c from case where Id in :caseIdSet];
            for(case caseObj: caseList)
            {
                caseMap.put(caseObj.Id,caseObj);
            }
        }
        for (EmailMessage messageObj: newEmailMessageList){  

            Case caseObj = caseMap.get(messageObj.ParentId);
            caseObj.Source_Email__c = MessageObj.ToAddress;
            System.debug('messageObj.ToAddress abc '+messageObj.ToAddress);
            if(caseObj.status !='New' && caseObj.Status_Alert__c != 'New Email Received' && messageObj.incoming && messageObj.toAddress!= null ){
                caseobj.status_Alert__c = 'New Email received';                
               
                Database.DMLOptions dmlOpts = new Database.DMLOptions();
                dmlOpts.assignmentRuleHeader.useDefaultRule= true;
                caseobj.setOptions(dmlOpts); 
                
                newCaseList.add(caseObj);                
            }          
            else if(!messageObj.incoming){
                caseobj.status_Alert__c = '';
                newCaseList.add(caseObj);
            }
        }
        if(newCaseList.size() > 0){
            update newCaseList;
        }        
    }
}

I'm calling the method in After insert and After update context.
Can any one please help me out on this?
 
SUCHARITA MONDALSUCHARITA MONDAL
Hi Allen,
Check the below link:  (Check Jeff May's comments in this chain)
https://developer.salesforce.com/forums/?id=906F00000008wj7IAA

Thanks,
Sucharita
Allen2Allen2

This is different case that you are trying to refer.

Thanks!

Allen