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... – Matt and Neil Jul 29 '20 at 15:46
  • trigger is process builder and it looks at that field only true value starts the process. – Marcin Trofiniak Jul 30 '20 at 9:39
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. – Marcin Trofiniak Jul 30 '20 at 9:38
  • I posted code I used to try it out. – Marcin Trofiniak Jul 30 '20 at 9:43

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.