4

what I need to do is trigger assignment rule for Cases created from Action on Opportunity Layout. Since it is custom action there is no "Assign with active case assignment rule button available". So I found and tried to use DmlOptions.AssignmentRuleHeader.

Here is code:

public class MEPL_AssignmentRuleTriggerer {

    @InvocableMethod
    public static void CaseStandardAssignmentRule( List<Id> CasesIds ) {
        AssignmentRule AR = new AssignmentRule();
        AR = [SELECT id from AssignmentRule WHERE sObjectType = 'Case' AND Active = true limit 1];
        
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.assignmentRuleHeader.assignmentRuleId = AR.Id;
        List<Case> toUpdate = new List<Case>();
        for( Id singleId : CasesIds ) {
            toUpdate.add( new Case( id = singleId, Assign_using_active_assignment_rule__c = false ) );
            toUpdate[toUpdate.size()-1].setOptions(dmo);
        }
        update toUpdate;
    }
}

I know code is working cause field changed in code to false equals false. Yet owner is not changed with use of assingment rule. When I create case standard way there is no problem with Rule. What do I do wrong here?

Update 1

Tried this code:

public class MEPL_AssignmentRuleTriggerer {

    @InvocableMethod
    public static void CaseStandardAssignmentRule( List<Id> CasesIds ) {
        AssignmentRule AR = new AssignmentRule();
        AR = [SELECT id from AssignmentRule WHERE sObjectType = 'Case' AND Active = true limit 1];
        
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.assignmentRuleHeader.assignmentRuleId = AR.Id;
        List<Case> toUpdate = new List<Case>();
        for( Id singleId : CasesIds ) {
            toUpdate.add( new Case( id = singleId, Assign_using_active_assignment_rule__c = false ) );
            toUpdate[toUpdate.size()-1].setOptions(dmo);
        }
        Database.update(toUpdate, dmo);
    }
}

It didn't work as well. Again value in checkbox changed by code but assignment process not triggered.

2
  • Regarding Assign_using_active_assignment_rule__c -- is that custom field being used inside by a trigger to specify the assignment rule behaviour before update or similar? Triggers are one context where modifying DML options has no effect... Commented Jul 29, 2020 at 15:46
  • trigger is process builder and it looks at that field only true value starts the process. Commented Jul 30, 2020 at 9:39

2 Answers 2

1

The sObject method setOptions does not work (at least, I've never seen it work). Use the Database.update method instead:

Database.update(toUpdate, dmo);
2
  • Tried that way as well and it didn't work. Commented Jul 30, 2020 at 9:38
  • I posted code I used to try it out. Commented Jul 30, 2020 at 9:43
0

Move to logic code to Queuable.

public with sharing class AssignOwnerQueue implements Queueable{

   List<Id> casesIds;

   public AssignOwnerQueue(List<Id> casesIds){
      this.casesIds= casesIds;
   }

   public void execute(QueueableContext context) {
        AssignmentRule AR = new AssignmentRule();
        AR = [SELECT id from AssignmentRule WHERE sObjectType = 'Case' AND Active = true limit 1];
        
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.assignmentRuleHeader.assignmentRuleId = AR.Id;
        List<Case> toUpdate = new List<Case>();
        for( Id singleId : CasesIds ) {
            toUpdate.add( new Case( id = singleId, Assign_using_active_assignment_rule__c = false ) );
            toUpdate[toUpdate.size()-1].setOptions(dmo);
        }
        Database.update(toUpdate, dmo);

   }
}

public class MEPL_AssignmentRuleTriggerer {

    @InvocableMethod
    public static void CaseStandardAssignmentRule( List<Id> caseIds) {
         ID jobID = System.enqueueJob(new AssignOwnerQueue(caseIds));
    }
}

You must log in to answer this question.

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