1

I need to trigger case assignment rules on update from a lightning page. I referred to this link to do that via trigger. After a little modification, I called the below method in before update event, but it doesn't work:

public static void escalateCaseToQueue(List<Case> lstOfCasesforAssignmentRules){
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule = TRUE;
    for(Case cs: lstOfCasesforAssignmentRules) {
        cs.setOptions(dmo);
    }
}

Why does this not work although this is called in the before update event?

I am currently using the below code in the before insert event and this is working:

public static void escalateCaseToQueue(Set<Id> setOfCaseIdsforAssignmentRules){
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule = TRUE; 
    List<Case> lstOfCasesforAssignmentRules = [SELECT Id FROM Case WHERE Id IN :setOfCaseIdsforAssignmentRules];
    Database.update(lstOfCasesforAssignmentRules, dmo);
}

Since I am doing a DML in the before update event, is there a better way than this?

Also, is there any alternative way to trigger assignment rules on update from lightning page without using Apex?

  • I have updated the question. DMLs don't work in before trigger, and I had copied the wrong code in the 2nd case. – Sagnik Chattopadhyay Nov 20 '17 at 14:17
1

DML options cannot affect records already in a trigger, as those records' settings were set by the calling context (e.g. a user interface update or API call). By using a DML statement in the trigger, you're recursively updating the records with the new options.

If possible, you should avoid that pattern and set the correct options from the calling context instead. Recursive triggers are more likely to fail and take longer to process.

| improve this answer | |
  • In this case, how can I set the options from the calling context? The record is getting edited in the UI. I could not find a way to do this without the DML. – Sagnik Chattopadhyay Nov 20 '17 at 14:15
  • @SagnikChattopadhyay Add the "Use Assignment Rule" option on the Page Layout, and then the users can choose to run the assignment rule in the UI. – sfdcfox Nov 20 '17 at 14:25
  • That checkbox is not available in the Lightning Record Page, is it? – Sagnik Chattopadhyay Nov 20 '17 at 14:26
  • @SagnikChattopadhyay It looks like it is. I suggest that you might be asking the wrong question (see xyproblem.info). You seem to be trying to working around a non-existent problem. – sfdcfox Nov 20 '17 at 14:32
  • I tried, but couldn't find it for lightning record pages, though it is available in classic page layouts. And my question is mainly concerned with the behaviour of the DMLOption in the trigger. – Sagnik Chattopadhyay Nov 20 '17 at 14:43
0

It is written in the documentation of DMLOptions:

DMLOptions settings take effect only for record operations performed using Apex DML and not through the Salesforce user interface.

| improve this answer | |

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.