+ Start a Discussion
jayamjayam 

Re:mixed_Dml_Operation

hi

 

I get the following Exception when  run my test class.

 

 

My test class code is

 

 

 

 

@isTest
private class Testinglead{

    static testmethod void testingowner(){
    
         Group g = new Group(Type='Queue', Name='Queue name');
        insert g;

        QueueSObject q = new QueueSObject(SobjectType='Lead', QueueId=g.Id);
        insert q;
            
          
               Lead obj=new Lead();
          obj.Lastname='XYZ';
          obj.Company='Appshark';
          obj.Description='xx';
                insert obj;
     
          obj.Lastname='XY';
          obj.Company='Appshark';
          obj.Description='xx';
          obj.ownerid=q.Id;
          obj.ownername__c='Charan Vuyyur';
          update obj;
     
     }
}

 

 

The Exception is:

 

 

 

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: Lead: []

 

 

how can I solve this ?

Is it possible to solve or not?

 

Please Help me?

it's urgent.

 

 

 

bob_buzzardbob_buzzard

You are getting this error because your test is creating a group and queue, which are setup objects, and then attempting to create a lead which is an application object.

 

You can solve this by creating a user and executing the rest of the test as that user via the System.runAs method.

bob_buzzardbob_buzzard

Here's an example from one of my dev orgs:

 

 

Profile p = [select id from profile where name='System Administrator'];
User u = new User(alias = 'utest', email='unit.test@unit.test.com',
      emailencodingkey='UTF-8', lastname='Unit Test', 
      languagelocalekey='en_US',
      localesidkey='en_GB', profileid = p.Id,
      timezonesidkey='Europe/London', 
      username='unit.test@unit.test.com');
                 
System.runAs(u)
{
    // create leads etc here and continue test
}
     

 

 

jayamjayam

Thanks for ur Reply Bob.

I changed my code Acc. to  ur suggestion .

 

Here is My code.

 

 

@isTest
private class Testinglead
{
    static testmethod void testingowner()
    {
      Profile p = [select id from profile where name='System Administrator'];
        User u = new User(alias = 'utest', email='unit.test@unit.test.com',
      emailencodingkey='UTF-8', lastname='Unit Test',
      languagelocalekey='en_US',
      localesidkey='en_GB', profileid = p.Id,
      timezonesidkey='Europe/London',
      username='unit.test@unit.test.com');
      
                 
System.runAs(u)
{
    // create leads etc here and continue test
     Lead obj=new Lead();
      obj.Lastname='XYZ';
      obj.Company='Appshark';
      obj.Description='xx';
      //obj.ownerid=u[0].id;
      insert obj;

       Group g = new Group(Type='Queue', Name='Queue name');
    insert g;
          system.debug('group idddddddddddd'+g.id);

    QueueSObject q = new QueueSObject(SobjectType='Lead', QueueId=g.Id);
    
    insert q;
    system.debug('ghdfajdgj'+q.id);
    obj.Lastname='XY';
      obj.Company='Appshark';
      obj.Description='xx';
      obj.ownerid=q.Id;
      obj.ownername__c='Charan Vuyyur';
      update obj;

 
}
}
       
}

 

but it gives FIELD_INTEGRITY_EXCEPTION  when I assign the queue id As owner id of Lead.

 

What can I do?

 

Please Reply me Bob.

jayamjayam

Thanks bob, I solved My Problem.

 

Thanks Alot Bob.

Sanchivan SivadasanSanchivan Sivadasan

How did you resolve this problem? I am getting the FIELD_INTEGRITY_EXCEPTION, Owner ID: id value of incorrect type: as well. Thanks.

Sanchivan SivadasanSanchivan Sivadasan

I figured out the issue. The issue was we should be assigning the groupQueue id instead of queueObjectLead id. Hope this helps someone in the future.

Ravi NarayananRavi Narayanan
You should use Future callouts if you are going to update/insert/delete  Setup and NonSetup Objects in same transaction.