+ Start a Discussion
Jeff Hutchinson 17Jeff Hutchinson 17 

How do I prevent Assignment Rules from firing in an InvocableMethod?

We are using Process Builder to call an InvocableMethod when the stage changes on an opportunity. The method creates a case and sends an email to its owner. When the opportunity is updated via the API, after the case is created the assignment rules run and reassign the case to a queue. This causes an error when the next block of code tries to send an email to the queue. I can update the opportunity via the UI without any issues.
// Create Case
        for (Opportunity opps : oppsList) {
            Case c = new Case(BusinessHoursId = '01mf20000008TQr', AccountId = opps.AccountId,
                    Case_Reason__c = 'Refer to Dentist', Description = opps.Account.Name + ' - Refer to Dentist', Destination__c = 'Support',
                    Origin = 'In-House', Status = 'Closed', Subject = opps.Account.Name + ' - Refer to Dentist', Type = 'Patient Updates',
                    OwnerId = accountMap.get(opps.AccountId).Patient_Care_Contact__c,
                    ContactId = accIdToContactMap.get(opps.AccountId).Id);

        System.debug('casesToInsert: ' + casesToInsert);
        insert casesToInsert;
        List<Case> insertedCases = [SELECT Id, AccountId, ContactId, OwnerId, Account.Patient_Care_Contact__c FROM Case WHERE Id IN :casesToInsert];
        System.debug('insertedCases: ' + insertedCases);

        // Email Pt
        EmailTemplate et = [Select Id from EmailTemplate where DeveloperName = 'Refer_to_Dentist'];
        OrgWideEmailAddress owea = [SELECT Id FROM OrgWideEmailAddress WHERE Address = 'support@email.byteme.com' LIMIT 1];
        List<Messaging.SingleEmailMessage> emailToSend = new List<Messaging.SingleEmailMessage>();
        for (Case c : insertedCases) {
            String[] recipients = new String[]{c.ContactId, c.OwnerId, 'mtamayo@byteme.com'};
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
As I understand it, Assignment Rules should not be running here. I realize there are changes I could make to the Assignment Rules to prevent the reassignment but I would prefer for everything to work as intended rather than use a work-around. I have an open case with Salesforce but because we're not a Premier Service member they haven't given much help.

Can someone please confirm that the assignment rules should not be running here, or if they should be, tell me how I can disable them?
Prabhat Sharma 6Prabhat Sharma 6
Hi Jeff,

When creating cases from Apex, you can specify DMLOptions to prevent the assignment rules from firing. Something like this:
Database.DMLOptions options = new Database.DMLOptions();
options.assignmentRuleHeader.useDefaultRule = false;
insert casesToInsert;
Add this before line 13, it should work.