Okay, I know very well by now that SFDC does not want and often does not allow us simply insert and/or update Setup and/or "Non-setup" sObjects in the same context anymore, something I used to do with impunity when I first started developing Apex.

I've also noticed that it is increasingly neurotic in finding and diagnosing these exceptions, especially when tests are run from the web-ui (instead of Eclipse or the Ant migration tool).

I'm curious whether this is "just" a security concern (i.e. someone might abuse mixed DML to create a backdoor) or whether there are other considerations that would/should make such mixing undesirable?

1 Answer 1


You can now use System.runAs to skirt around the limitation, but only for tests. Note that using System.runAs counts as a DML statement, so you cannot infinitely jump around between users. See the documentation on testing Using the runAs Method.

The "why" is that many setup objects can't be mixed with normal records because of the complexity involved with setup objects. For example, creating a new user normally causes a recalculation of sharing rules for that user, and changing a profile's "View All Data" permission might also cause a massive recalculation operation.

In addition, I believe such calculations are deferred, which means that a change to a user or profile might result in allowing users to modify records they otherwise shouldn't be able to as a result of the change, but only during the same transaction.

  • 1
    At DF13 in the Meet the Developers session your reason of the change to a user or profile was brought up as the primary reason. Dec 3, 2013 at 15:32
  • I agree. Any test I would write would be for specifically testing limited permission profiles. It's not normally necessary to use other "setup" objects in casual testing.
    – sfdcfox
    Dec 3, 2013 at 16:09
  • @sfdcfox, I can't and won't quibble about normal, but I'm finding more and more that employers and clients are making heavy use of the custom settings objects, which I imagine you realize are "setup" objects. As this data ties into triggers and validation rules, I often find it is necessary to populate these (often many of these) for my tests. Dec 3, 2013 at 17:37
  • @sfdcfox, I am aware that I can use System.runAs, but I'm finding it annoying to fit/retrofit it into my code to make sure my tests pass... and while I didn't get specific errors from too many runAs calls, I did notice erratic results, as if either some data was being forgotten or data was being updated out of the expected sequence. Dec 3, 2013 at 17:39

You must log in to answer this question.

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