-1

Apex trigger TF_UserTrigger caused an unexpected exception, contact your administrator: TF_UserTrigger: execution of AfterUpdate 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): WorkflowQueue__c, original object: User: []: Class.TF_WorkflowQueue.Add: line 25, column 1

This quite clear alert. I somehow tried to alter the original User object and thus I got such ugly message. But how? There is load of code so I will try to explain it by word firstly:

I tried to deactivate User. This started chain of events. At first my apex look for all Contacts owner by this User, do some additional checks and then it queue it up into WorkflowQueue__c. Items from this queue are taken by scheduler and sent into my system. Important is, the whole process doesn't affect original User in any means.

Moment of crash is on insert line:

public void Add(string data, boolean multimessage)
{
    if(data == null || String.isEmpty(data))
    {
        this.logger.Debug('Variable \'Data\' should not be null.');
        throw new MoraviaArgumentException('Variable \'Data\' should not be null.');
    }

    WorkflowQueue__c item = new WorkflowQueue__c(Data__c = data, Multimessage__c = multimessage); 

    insert item;
}

How the object looks like: enter image description here

And still the exception tells me I tried to change the User somehow. I would be grateful for any hint of what I am missing or misunderstanding.

  • do you have a WFR or declarative process that is trying to change the WorkflowQueue__c.Owner? – tagCincy Dec 20 '17 at 13:14
  • @tagCincy Nope. You can see everything what is done with WorkflowQueue__c before the exception occures. After that there is processing mechanism which only reads single records. No altering at all. – Qerts Dec 20 '17 at 13:21
  • I would examine the debug logs a little closer, if the User (or any other setup object) is manipulated in the same transaction (not just the direct apex code), it will pop this error. Normally when I see it, it is a WFR that one of the unit admins put in without my knowledge. – tagCincy Dec 20 '17 at 13:46
  • 1
    Have you got a trigger on the standard User object that is leading to this Add method? – TSmith Dec 20 '17 at 14:09
  • @tagCincy I'll go for the logs to see if there are any system changes. As I saw in my own logging, other SObjects are not manipulated in my apex code. – Qerts Dec 20 '17 at 14:24
0

When you tried to deactivate a user, the code in your post ran, and since it's part of the same transaction, you got a mixed DML error message, because while you were updating the user, you tried to insert another object.

The key word here is "Transaction" - defined as:

An Apex transaction represents a set of operations that are executed as a single unit.

All DML operations in a transaction either complete successfully, or if an error occurs in one operation, the entire transaction is rolled back and no data is committed to the database.

The boundary of a transaction can be a trigger, a class method, an anonymous block of code, a Visualforce page, or a custom Web service method.

Your transaction started when you updated the user. Your trigger executes in the same transaction. During this trigger, you insert a non-setup object- causing your exception, since your mixing setup & non-setup objects.

Instead of calling this method in the same context, use the @future annotation. Itll cause the method to be executed a few minutes after the trigger runs, and will be in its own context, allowing you to insert a non-setup object without incurring an exception.

  • I solved it this way regardless on this answer, but tbh didn't know why it's working or rather what was wrong. Thanks for very nice explanation. – Qerts Dec 20 '17 at 14:45
0

You cannot create/update setup and non-setup object records inside the same method. as in your case WorkflowQueue__c is a non-setup object.please see whether you are creating other setup or non-setup objects in the same transaction

  • This is the only insertion. – Qerts Dec 20 '17 at 14:20

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.