+ Start a Discussion
Mahatma VijayMahatma Vijay 

Error during test!

I did a test run on this class and it gave me an error. Can someone guide me as to how I can run this test successfully?
@isTest
private class F8_Test_AssignmentGroupQueue {

    static testMethod void F8_Test_AssignmentGroupQueue() {
      Assignment_Group__c objAssignmentGroup = new Assignment_Group__c(Name='Test_AG');
      insert objAssignmentGroup;
        
        Group g1 = new Group(Name='group name', type='Queue');
      insert g1;
        QueuesObject q1 = new QueueSObject(QueueID = g1.id, SobjectType = 'Assignment_Group__c');
        insert q1;
        
        Group g2 = new Group(Name='group name2', type='Queue');
      insert g2;
        QueuesObject q2 = new QueueSObject(QueueID = g2.id, SobjectType = 'Assignment_Group__c');        
        insert q2;
        
        Group g3 = new Group(Name='group name3', type='Queue');
      insert g3;
        QueuesObject q3 = new QueueSObject(QueueID = g3.id, SobjectType = 'Assignment_Group__c');        
        insert q3;
        
        Assignment_Group_Queue__c objAssignmentGroupQueue = new Assignment_Group_Queue__c(Name = 'group name', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
        insert objAssignmentGroupQueue;
                                       
    Assignment_Group_Queue__c objAssignmentGroupQueue2 = new Assignment_Group_Queue__c(Name = 'group name2', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
        insert objAssignmentGroupQueue2;
        
        objAssignmentGroupQueue.Active__c = 'False';
        update  objAssignmentGroupQueue;
        
        objAssignmentGroupQueue.Name = 'group name3';
        update objAssignmentGroupQueue;
        
        try{
          objAssignmentGroupQueue.Name = 'group name2';
          update objAssignmentGroupQueue;  
        }
        catch(Exception ex){
          system.debug('## Exception is ::' + ex);  
        }
        
        Assignment_Group_Queue__c objAssignmentGroupQueue3 = new Assignment_Group_Queue__c();
        try{
          objAssignmentGroupQueue3 = new Assignment_Group_Queue__c(Name = 'group name2', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
          insert objAssignmentGroupQueue3;
        }
        catch(Exception ex){
          system.debug('## Exception is ::' + ex);
          System.assertEquals(objAssignmentGroupQueue3.Id, null);    
        }
        
        try{
          objAssignmentGroupQueue3 = new Assignment_Group_Queue__c(Name = 'group name23', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
          insert objAssignmentGroupQueue3;
        }
        catch(Exception ex){
          system.debug('## Exception is ::' + ex);
          System.assertEquals(objAssignmentGroupQueue3.Id, null);    
        }
        
        try{
          objAssignmentGroupQueue.Name = 'group name23';
          update objAssignmentGroupQueue;  
        }
        catch(Exception ex){
          system.debug('## Exception is ::' + ex);  
        }    
    }
}

Errors:

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

Stack Trace:

Class.F8_Test_AssignmentGroupQueue.F8_Test_AssignmentGroupQueue: line 32, column 1
 
Best Answer chosen by Mahatma Vijay
Mahatma VijayMahatma Vijay
Yes! One of my friend helped me with this code change and it worked. I'm attaching the code so it helps everyone.
 
@isTest
private class F8_Test_AssignmentGroupQueue {

    static testMethod void F8_Test_AssignmentGroupQueue() {
      //changes made to avoid MIXED_DML_OPERATION error.: Revanth
       Profile p = [select id from profile where name='System Administrator'];
        /*User user = new User();
        user.Alias = 'test';
        user.Email = 'test@aruvio.com';
        user.EmailEncodingKey = 'UTF-8';
        user.LastName = 'tester';
        user.LanguageLocaleKey = 'en_US';
        user.LocaleSidKey = 'en_US';
        user.ProfileId = p.Id;
        user.TimeZoneSidKey = 'America/Los_Angeles';
        user.IsActive = true;         
        
        insert user; */
        
        User user=[SELECT Id FROM User WHERE ProfileId=:p.id and user.IsActive = true limit 1];
                         
        System.runAs(user)
        {       
          Assignment_Group__c objAssignmentGroup = new Assignment_Group__c(Name='Test_AG');
          insert objAssignmentGroup;
          Group g1 = new Group(Name='group name', type='Queue');
          insert g1;
          QueuesObject q1 = new QueueSObject(QueueID = g1.id, SobjectType = 'Assignment_Group__c');
          insert q1;
          Group g2 = new Group(Name='group name2', type='Queue');
          insert g2;
          QueuesObject q2 = new QueueSObject(QueueID = g2.id, SobjectType = 'Assignment_Group__c');        
          insert q2;
          Group g3 = new Group(Name='group name3', type='Queue');
          insert g3;
          QueuesObject q3 = new QueueSObject(QueueID = g3.id, SobjectType = 'Assignment_Group__c');        
          insert q3;
          Assignment_Group_Queue__c objAssignmentGroupQueue = new Assignment_Group_Queue__c(Name = 'group name', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
            insert objAssignmentGroupQueue;
            Assignment_Group_Queue__c objAssignmentGroupQueue2 = new Assignment_Group_Queue__c(Name = 'group name2', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
            insert objAssignmentGroupQueue2;
            
            objAssignmentGroupQueue.Active__c = 'False';
            update  objAssignmentGroupQueue;
            
            objAssignmentGroupQueue.Name = 'group name3';
            update objAssignmentGroupQueue;
            
            try{
              objAssignmentGroupQueue.Name = 'group name2';
              update objAssignmentGroupQueue;  
            }
            catch(Exception ex){
              system.debug('## Exception is ::' + ex);  
            }
            
            Assignment_Group_Queue__c objAssignmentGroupQueue3 = new Assignment_Group_Queue__c();
            try{
              objAssignmentGroupQueue3 = new Assignment_Group_Queue__c(Name = 'group name2', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
              insert objAssignmentGroupQueue3;
            }
            catch(Exception ex){
              system.debug('## Exception is ::' + ex);
              System.assertEquals(objAssignmentGroupQueue3.Id, null);    
            }
            
            try{
              objAssignmentGroupQueue3 = new Assignment_Group_Queue__c(Name = 'group name23', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
              insert objAssignmentGroupQueue3;
            }
            catch(Exception ex){
              system.debug('## Exception is ::' + ex);
              System.assertEquals(objAssignmentGroupQueue3.Id, null);    
            }
            
            try{
              objAssignmentGroupQueue.Name = 'group name23';
              update objAssignmentGroupQueue;  
            }
            catch(Exception ex){
              system.debug('## Exception is ::' + ex);  
            } 
        }       
    }
}

Thanks Mahesh for doing the best in helping me.

All Answers

Mahesh DMahesh D
Hi Revanth,

You are getting this error because your test is creating a group and queue, which are setup objects, and then attempting to create a Custom Object 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.

Below is the modified code:

 
@isTest
private class F8_Test_AssignmentGroupQueue {

    static testMethod void F8_Test_AssignmentGroupQueue() {
      Assignment_Group__c objAssignmentGroup = new Assignment_Group__c(Name='Test_AG');
      insert objAssignmentGroup;
        
        Group g1 = new Group(Name='group name', type='Queue');
      insert g1;
        QueuesObject q1 = new QueueSObject(QueueID = g1.id, SobjectType = 'Assignment_Group__c');
        insert q1;
        
        Group g2 = new Group(Name='group name2', type='Queue');
      insert g2;
        QueuesObject q2 = new QueueSObject(QueueID = g2.id, SobjectType = 'Assignment_Group__c');        
        insert q2;
        
        Group g3 = new Group(Name='group name3', type='Queue');
      insert g3;
        QueuesObject q3 = new QueueSObject(QueueID = g3.id, SobjectType = 'Assignment_Group__c');        
        insert q3;
        
		Profile p = [select id from profile where name='System Administrator'];
		User user = new User();
        user.Alias = 'test';
        user.Email = 'test@aruvio.com';
        user.EmailEncodingKey = 'UTF-8';
        user.LastName = 'tester';
        user.LanguageLocaleKey = 'en_US';
        user.LocaleSidKey = 'en_US';
        user.ProfileId = p.Id;
        user.TimeZoneSidKey = 'America/Los_Angeles';
        user.IsActive = true;         
        
        insert user;
						 
		System.runAs(u)
		{		
			Assignment_Group_Queue__c objAssignmentGroupQueue = new Assignment_Group_Queue__c(Name = 'group name', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
			insert objAssignmentGroupQueue;
										   
		Assignment_Group_Queue__c objAssignmentGroupQueue2 = new Assignment_Group_Queue__c(Name = 'group name2', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
			insert objAssignmentGroupQueue2;
			
			objAssignmentGroupQueue.Active__c = 'False';
			update  objAssignmentGroupQueue;
			
			objAssignmentGroupQueue.Name = 'group name3';
			update objAssignmentGroupQueue;
			
			try{
			  objAssignmentGroupQueue.Name = 'group name2';
			  update objAssignmentGroupQueue;  
			}
			catch(Exception ex){
			  system.debug('## Exception is ::' + ex);  
			}
			
			Assignment_Group_Queue__c objAssignmentGroupQueue3 = new Assignment_Group_Queue__c();
			try{
			  objAssignmentGroupQueue3 = new Assignment_Group_Queue__c(Name = 'group name2', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
			  insert objAssignmentGroupQueue3;
			}
			catch(Exception ex){
			  system.debug('## Exception is ::' + ex);
			  System.assertEquals(objAssignmentGroupQueue3.Id, null);    
			}
			
			try{
			  objAssignmentGroupQueue3 = new Assignment_Group_Queue__c(Name = 'group name23', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
			  insert objAssignmentGroupQueue3;
			}
			catch(Exception ex){
			  system.debug('## Exception is ::' + ex);
			  System.assertEquals(objAssignmentGroupQueue3.Id, null);    
			}
			
			try{
			  objAssignmentGroupQueue.Name = 'group name23';
			  update objAssignmentGroupQueue;  
			}
			catch(Exception ex){
			  system.debug('## Exception is ::' + ex);  
			} 
		}		
    }
}
Please do let me know if it helps you.

Regards,
Mahesh
Mahatma VijayMahatma Vijay
Hi Mahesh, 

Thank you so much for the help. I tried the code but I got this error. I'm new to Salesforce Development and can you help with this following error as well?

Error: Compile Error: Variable does not exist: u at line 58 column 22
Mahesh DMahesh D
Replace u with user at line 37

System.runAs(u)

to

System.runAs(user)

Regards,
Mahesh
Mahatma VijayMahatma Vijay
Hi Mahesh, 

Still getting same errors

Errors:

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

Stack Trace:

Class.F8_Test_AssignmentGroupQueue.F8_Test_AssignmentGroupQueue: line 32, column 1
Mahesh DMahesh D
Try this code:
 
@isTest
private class F8_Test_AssignmentGroupQueue {

    static testMethod void F8_Test_AssignmentGroupQueue() {
      
        
        Group g1 = new Group(Name='group name', type='Queue');
      insert g1;
        QueuesObject q1 = new QueueSObject(QueueID = g1.id, SobjectType = 'Assignment_Group__c');
        insert q1;
        
        Group g2 = new Group(Name='group name2', type='Queue');
      insert g2;
        QueuesObject q2 = new QueueSObject(QueueID = g2.id, SobjectType = 'Assignment_Group__c');        
        insert q2;
        
        Group g3 = new Group(Name='group name3', type='Queue');
      insert g3;
        QueuesObject q3 = new QueueSObject(QueueID = g3.id, SobjectType = 'Assignment_Group__c');        
        insert q3;
        
		Profile p = [select id from profile where name='System Administrator'];
		User user = new User();
        user.Alias = 'test';
        user.Email = 'test@aruvio.com';
        user.EmailEncodingKey = 'UTF-8';
        user.LastName = 'tester';
        user.LanguageLocaleKey = 'en_US';
        user.LocaleSidKey = 'en_US';
        user.ProfileId = p.Id;
        user.TimeZoneSidKey = 'America/Los_Angeles';
        user.IsActive = true;         
        
        insert user;
						 
		System.runAs(user)
		{		
			Assignment_Group__c objAssignmentGroup = new Assignment_Group__c(Name='Test_AG');
			insert objAssignmentGroup;
			Assignment_Group_Queue__c objAssignmentGroupQueue = new Assignment_Group_Queue__c(Name = 'group name', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
			insert objAssignmentGroupQueue;
										   
		Assignment_Group_Queue__c objAssignmentGroupQueue2 = new Assignment_Group_Queue__c(Name = 'group name2', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
			insert objAssignmentGroupQueue2;
			
			objAssignmentGroupQueue.Active__c = 'False';
			update  objAssignmentGroupQueue;
			
			objAssignmentGroupQueue.Name = 'group name3';
			update objAssignmentGroupQueue;
			
			try{
			  objAssignmentGroupQueue.Name = 'group name2';
			  update objAssignmentGroupQueue;  
			}
			catch(Exception ex){
			  system.debug('## Exception is ::' + ex);  
			}
			
			Assignment_Group_Queue__c objAssignmentGroupQueue3 = new Assignment_Group_Queue__c();
			try{
			  objAssignmentGroupQueue3 = new Assignment_Group_Queue__c(Name = 'group name2', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
			  insert objAssignmentGroupQueue3;
			}
			catch(Exception ex){
			  system.debug('## Exception is ::' + ex);
			  System.assertEquals(objAssignmentGroupQueue3.Id, null);    
			}
			
			try{
			  objAssignmentGroupQueue3 = new Assignment_Group_Queue__c(Name = 'group name23', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
			  insert objAssignmentGroupQueue3;
			}
			catch(Exception ex){
			  system.debug('## Exception is ::' + ex);
			  System.assertEquals(objAssignmentGroupQueue3.Id, null);    
			}
			
			try{
			  objAssignmentGroupQueue.Name = 'group name23';
			  update objAssignmentGroupQueue;  
			}
			catch(Exception ex){
			  system.debug('## Exception is ::' + ex);  
			} 
		}		
    }
}

Regards,
Mahesh
Mahatma VijayMahatma Vijay
Hi Mahesh,

I'm still getting the same error message.
Mahatma VijayMahatma Vijay
Yes! One of my friend helped me with this code change and it worked. I'm attaching the code so it helps everyone.
 
@isTest
private class F8_Test_AssignmentGroupQueue {

    static testMethod void F8_Test_AssignmentGroupQueue() {
      //changes made to avoid MIXED_DML_OPERATION error.: Revanth
       Profile p = [select id from profile where name='System Administrator'];
        /*User user = new User();
        user.Alias = 'test';
        user.Email = 'test@aruvio.com';
        user.EmailEncodingKey = 'UTF-8';
        user.LastName = 'tester';
        user.LanguageLocaleKey = 'en_US';
        user.LocaleSidKey = 'en_US';
        user.ProfileId = p.Id;
        user.TimeZoneSidKey = 'America/Los_Angeles';
        user.IsActive = true;         
        
        insert user; */
        
        User user=[SELECT Id FROM User WHERE ProfileId=:p.id and user.IsActive = true limit 1];
                         
        System.runAs(user)
        {       
          Assignment_Group__c objAssignmentGroup = new Assignment_Group__c(Name='Test_AG');
          insert objAssignmentGroup;
          Group g1 = new Group(Name='group name', type='Queue');
          insert g1;
          QueuesObject q1 = new QueueSObject(QueueID = g1.id, SobjectType = 'Assignment_Group__c');
          insert q1;
          Group g2 = new Group(Name='group name2', type='Queue');
          insert g2;
          QueuesObject q2 = new QueueSObject(QueueID = g2.id, SobjectType = 'Assignment_Group__c');        
          insert q2;
          Group g3 = new Group(Name='group name3', type='Queue');
          insert g3;
          QueuesObject q3 = new QueueSObject(QueueID = g3.id, SobjectType = 'Assignment_Group__c');        
          insert q3;
          Assignment_Group_Queue__c objAssignmentGroupQueue = new Assignment_Group_Queue__c(Name = 'group name', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
            insert objAssignmentGroupQueue;
            Assignment_Group_Queue__c objAssignmentGroupQueue2 = new Assignment_Group_Queue__c(Name = 'group name2', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
            insert objAssignmentGroupQueue2;
            
            objAssignmentGroupQueue.Active__c = 'False';
            update  objAssignmentGroupQueue;
            
            objAssignmentGroupQueue.Name = 'group name3';
            update objAssignmentGroupQueue;
            
            try{
              objAssignmentGroupQueue.Name = 'group name2';
              update objAssignmentGroupQueue;  
            }
            catch(Exception ex){
              system.debug('## Exception is ::' + ex);  
            }
            
            Assignment_Group_Queue__c objAssignmentGroupQueue3 = new Assignment_Group_Queue__c();
            try{
              objAssignmentGroupQueue3 = new Assignment_Group_Queue__c(Name = 'group name2', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
              insert objAssignmentGroupQueue3;
            }
            catch(Exception ex){
              system.debug('## Exception is ::' + ex);
              System.assertEquals(objAssignmentGroupQueue3.Id, null);    
            }
            
            try{
              objAssignmentGroupQueue3 = new Assignment_Group_Queue__c(Name = 'group name23', Active__c = 'True', Assignment_Group_Name__c = objAssignmentGroup.Id, QueueId__c = g1.id, Valid_Queue__c = true);
              insert objAssignmentGroupQueue3;
            }
            catch(Exception ex){
              system.debug('## Exception is ::' + ex);
              System.assertEquals(objAssignmentGroupQueue3.Id, null);    
            }
            
            try{
              objAssignmentGroupQueue.Name = 'group name23';
              update objAssignmentGroupQueue;  
            }
            catch(Exception ex){
              system.debug('## Exception is ::' + ex);  
            } 
        }       
    }
}

Thanks Mahesh for doing the best in helping me.
This was selected as the best answer