0

I'm trying to do a trigger to create a queue from a custom object and I'm get this error:

Review all error messages below to correct your data. Apex trigger ProjectHandler caused an unexpected exception, contact your administrator: ProjectHandler: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): QueueSobject, original object: Project__c: []: Trigger.ProjectHandler: line 8, column 1

Where is the code:

trigger ProjectHandler on Project__c (after insert) {
    for (Project__c project : Trigger.new) {
        String queueName = project.Name;
        Group queue = new Group(Name=queueName, Type='Queue',DoesSendEmailToMembers=true);
        insert queue;

        QueuesObject qso = new QueueSObject(QueueID = queue.id, SobjectType = 'User_in_Project__c');
        insert qso;

    }
}

There is any workaround? Two different triggers don´t solve the problem either. I can not create a trigger on a QueueSObject too.

3 Answers 3

2

One workaround would be to use future methods to create the setup objects. Have a look at this post for more details on how to do it.

You also need to bulkify your code so that you don't have the inserts within a for loop.

0
0

You are hitting the Mix DML error which can be pretty common depending on the features you use in Salesforce. Here is some more information about why this error exists and what can be done to solve it: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_non_mix_sobjects.htm

Basically you will want to move the insertion of QueuesObject to a @future method so it can be inserted successfully.

You also have some other issues with your code that I'd like to point out. Any time more than 75 Project__c records are inserted you will get an error. You will want to 'bulkify' your code. See this link: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bulk_idioms.htm

0
0

The comment was long, so posting as an answer. Sorry.

@ future would work in this scenario. I have one more step that depends on @ future. So here is my scenario - Create Queue on the fly, assign it to "Case" object. So far so good with solution mentioned above. But now, I need to create new case and use that Queue as owner id. I run into same issue if I try to create case in @ future method. And Salesforce won;t allow calling @ future methods from another @future methods, otherwise I would have move "Case Creation" into another @future method.

I tried 2 @ future methods, first one would assign Queue to Object and other one would Create Case. But that did not work, because @ future method execution order is not guaranteed.

Long story short, here is what I need "DML on non-system object >> DML on system object >> DML on non-system object "

You must log in to answer this question.

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