3

I have a user trigger that upserts both setup objects and custom settings records for provisioning purposes, and have everything in @future methods to avoid mixed DML operation errors. All of our test classes are breaking however, as almost all of them insert users. Does anyone have a workaround for this, besides doing something like adding System.runAs to all other test classes?

5
  • Can you say more about why they are breaking? Logs would be helpful
    – Kevin P
    Feb 19, 2014 at 18:07
  • Sorry, they're all breaking due to hitting the mixed DML operation error when my trigger fires after users are being inserted in other test classes Feb 19, 2014 at 18:11
  • So you've a list<sObject> that you're throwing users, accounts and other objects on? then inserting that list fails ?
    – Kevin P
    Feb 19, 2014 at 18:15
  • Could you query for users instead of inserting them? Even with seeAllData = false, user queries are allowed.
    – greenstork
    Feb 19, 2014 at 18:23
  • Good point!!!!!
    – Eric Luu
    Feb 19, 2014 at 22:52

1 Answer 1

4

Gonna go on a hunch here.

I learned the hard way that Mixed object DML is a powerful, but quirky tool. One of the things I learned is that it acts like the list is split into lists by adjacent type. In other words, if your List contains elements of User, Account and Opportunity (U, A and O respectively) like this:

UAAAUUOOOUAUO

That list will be "broken" into 8 sub lists by the DML call like this:

U
AAA
UU
OOO
U
A
U
O

This can quickly hit the "max chunks" limit.

Thankfully, a quick list.sort(); call will result in a mix-dml list like this UUUUUAAAAOOOO, which is broken into only 3 "chunks".

Try sorting your list before insert/update/etc. See if that Helps?

1
  • Thanks Kevin, but that's not exactly the issue I'm running into (which I'm probably describing badly). Basically whats happening is this: 1. User Trigger fires, and calls my User class to see if user fits entry criteria. 2. User class calls GroupMember future method inserts/deletes a GroupMember record for that user 3. User class calls CustomSetting__c future method inserts/deletes a custom setting record for that user In my test class, I'm using the System.runAs method to avoid mixed dml operation errors when inserting test user records, but the other test classes are failing on insert Feb 19, 2014 at 23:09

You must log in to answer this question.

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