When I try to delete an OrderItem from an Order, I am getting a DML Mixed Exception. However, I am not updating any other object.

Am I missing something?

private class TestDeleteOrderItemEntry {

static void del(Id oli) {
    system.assertEquals(0, [SELECT Id FROM OrderItem].size());

static testMethod void testDelete(){


    Account a = new Account(Name='TestAccount');
    insert a;

    Contract c = new Contract(AccountId=a.Id, Name='TestContract',StartDate=system.today().toStartOfMonth(),ContractTerm=30,Status='Draft');
    insert c;

    c.Status = 'Activated';
    update c;

    Pricebook2 pb = new Pricebook2(Name='TestPriceBook');
    insert pb;

    Product2 prod = new Product2(Name = 'TestProduct');
    insert prod;

    Order o = new Order(AccountId=a.Id,ContractId=c.Id,EndDate=system.now().addDays(1).date());
    o.EffectiveDate  = system.today();
    o.Status = 'Draft';
    o.Pricebook2Id = pb.Id;
    o.EndDate = system.today();
    insert o;

    PricebookEntry standardPrice = new PricebookEntry(
    Pricebook2Id = Test.getStandardPricebookId(), Product2Id = prod.Id,
    UnitPrice = 10000, IsActive = true);
    insert standardPrice;

    PricebookEntry priceEntry = new PricebookEntry(
    Pricebook2Id = pb.Id, Product2Id = prod.Id,
    UnitPrice = 10000, IsActive = true, UseStandardPrice=true);        
    insert priceEntry;

    OrderItem item = new OrderItem(OrderId=o.Id, Quantity=1, UnitPrice=3, PriceBookEntryId=priceEntry.Id);
    insert item;





15:10:59:712 FATAL_ERROR System.DmlException: Delete failed. First exception on row 0 with id 8027A0000008qQYQAY; 
            first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted 
            after you have updated a non-setup object (or vice versa):
            CustomFieldDefinition, original object: OrderItem: []


  • 2
    My guess would be that even though you may only be deleting the orderItem in your test class. There may be other events within the same transaction (such as a trigger) that is updating a setup object somewhere. And therefore causing a Mixed DML opperation to take place. Please check any trigger or your test class. – Himanshu Apr 11 '16 at 20:34
  • If @Himanshu is correct(which I think he is), your going to need to build in a atFuture – EricSSH Apr 11 '16 at 20:42
  • 2
    the future method is defined in the test class? or am i misreading something? – cropredy Apr 11 '16 at 22:07
  • 2
    Without seeing your test setup code, I suspect that everyone is going to be chasing a red herring. Those queries are running on data that came from someplace & how they were created may be at the root of the issue. – crmprogdev Apr 11 '16 at 22:30
  • 1
    there is no reason why a testmethod should have to delete an sobject inserted by the code via a future method – cropredy Apr 12 '16 at 16:31

The cause of the issue was a custom roll-up field on the Contract. Go Figure. I was SUMing the total order amounts across all orders for a certain contract.

To fix this issue, I created a custom formula field "TotalAmount2" on an Order. Then created a roll-up field across that custom field on the Contract.

Hope this helps someone else.

| improve this answer | |
  • I'm still confirming things but I think this fixed our error: "DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): CustomFieldDefinition, original object: OrderItem". Thank you for sharing! (FYI I up-voted this from -1 to 0 - not sure who would downvote this). – Chris D. Emerson Mar 19 '19 at 11:51

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.