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
ashish sharma 188ashish sharma 188 

How to access and use owner assigned by case assignment rule in trigger fired by after insert event.

Hi Folks,
I am working on a scenario in which I have to send emails to someone on the basis of some criteria that are as:
if case owner = 'User'{
  • IF case origin = 'phone' || 'email' {
  •    send email to associated contact from user's CustomerCenter email ID.
  • }
  • else {
  •    send email to supplied email in web to case creation from user's CustomerCenter email ID.
}
if(case owner = 'QUEUE'){
  • IF case origin = 'phone' || 'email' {
  •    send email to associated contact from queue email Id.
  • }
  • else {
  •    send email to supplied email in web to case creation from queue email ID.
}
}


As we know, assignment rules run after triggers, so I am unable to access the owner assigned by assignment rule to that case. I have run the assignment rules manually inside trigger but still not able to see the owner assigned by assignment rules.
I am posting my code as well. 
trigger CaseOriginSendEmail on Case (after insert) {
    
    if(Trigger.isAfter && Trigger.isInsert){
     AssignmentRule  ar = new AssignmentRule();
     ar = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
     Case cs = Trigger.new[0];
     Case c = new Case(id = cs.id);
     Database.DMLOptions dmlOpts = new Database.DMLOptions();
     dmlOpts.assignmentRuleHeader.assignmentRuleId= ar.id;
     c.setOptions(dmlOpts);
     Database.upsert(c);
     c=null;
     c=[SELECT id,OwnerID,origin,contactID,contactEmail,suppliedEmail from Case where id =: cs.id];
     String ownerIdStr = c.OwnerId;
        System.debug(cs.OwnerId + '\n'+c.OwnerId);
     if(ownerIdStr.startsWith('005')){
         System.debug('in user');
         User u = [SELECT id, ContactCenter__c from User where id =: cs.OwnerId];
         ContactCenter__c cc = [SELECT ContactCenterEmail__c from ContactCenter__c where name__c =: u.ContactCenter__c];
         System.debug(cc.ContactCenterEmail__c);
         if(cs.Origin != 'web')
             MyEmail1.sendMail1(cs.ContactEmail, cc.ContactCenterEmail__c);
         else{
             if(cs.ContactId!=null)
                 MyEmail1.sendMail2(cs.ContactEmail, cc.ContactCenterEmail__c);
             else
                 MyEmail1.sendMail3(cs.SuppliedEmail, cc.ContactCenterEmail__c);
         }
     }
    else if(ownerIdStr.startsWith('00G')){
        System.debug('in queue');
        Group queue = new Group(id = cs.OwnerId);
        String queueEmail = queue.email;
        if(cs.Origin != 'web')
             MyEmail1.sendMail1(cs.ContactEmail, queueEmail);
         else{
             if(cs.ContactId!=null)
                 MyEmail1.sendMail2(cs.ContactEmail, queueEmail);
             else
                 MyEmail1.sendMail3(cs.SuppliedEmail, queueEmail);
         }
    }
	}
}

In above code ContactCenter__c is a custom setting which is linked to users on the basis of picklist vaues. I can't find any error in above code and no way to get the owner assigned by assignment rule in Trigger. Please help me out.
ashish sharma 188ashish sharma 188
User-added image
i have created some assignment rules as well for testing purpose. Please have a glance.