I have a class wherein I am inserting two different records at the same time. These records are for a custom setting object named Setting__c and a custom object named Item__c. This works well and behaves accordingly.

Meanwhile, I created a test class for this but I am encountering this error:

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): Item__c, original object: UserRole: []

I needed to cover the line for pertaining to the user role object. How can I resolve this?

Apex Class:

public class SettingClass {

    private ApexPages.StandardController con;
    public Setting__c settingRec {get;set;}
    public List<SettingWrapper> swraplist {get;set;} 
    public Integer rowToRemove {get;set;}
    public String selectedRole {get;set;}
    public String selectedStat {get;set;}

    public SettingClass(ApexPages.StandardController controller) {
        con = controller;
        this.settingRec = (Setting__c)controller.getRecord();
        swraplist = new List<SettingWrapper>();

    public void addNewRow() {
        List<SettingWrapper> temp_wrap = new List<SettingWrapper>();
        SettingWrapper newSetting = new SettingWrapper();

        Item__c newItemRecord = new Item__c();      
        newSetting.set1 = newItemRecord;
        newSetting.index = temp_wrap.size();
        for(SettingWrapper sw : temp_wrap) {

    public void removeRow() {
        List<SettingWrapper> rem_wrap = new List<SettingWrapper>();
        for(SettingWrapper sw : swraplist) {
        for(SettingWrapper sw2 : rem_wrap) {

    public List<SelectOption> getUserRoleList() {
        List<UserRole> urList = [SELECT Id, Name FROM UserRole];
        List<SelectOption> urOptionList = new List<SelectOption>();
        urOptionList.add(new SelectOption(' ',' '));

        for(UserRole ur : urList) {
            urOptionList.add(new SelectOption(ur.Id, ur.Name));    

        return urOptionList;

    public List<SelectOption> getStatList() {
        List<SelectOption> statOptionList = new List<SelectOption>();
        Schema.DescribeFieldResult statResults = Account.Status__c.getDescribe();

        List<Schema.PicklistEntry> ple = statResults.getPicklistValues();

        for(Schema.PicklistEntry f : ple) {
            statOptionList.add(new SelectOption(f.getLabel(), f.getValue()));    

        return statOptionList;

    public PageReference save() {

        List<SettingWrapper> swList = new List<SettingWrapper>();
        List<Item__c> newItemList = new List<Item__c>();

        for(SettingWrapper sw : swraplist) {

        UserRole ur = [SELECT Id, Name FROM UserRole WHERE Id =: selectedRole];  

        Setting__c newSetting = new Setting__c();

        newSetting.Name = settingRec.Name;
        newSetting.Status__c = selectedStat;
        newSetting.Role_ID__c = selectedRole;
        newSetting.Role_Name__c = ur.Name;

        insert newSetting;

        if(swList !=null && !swList.isEmpty()) {

            for(SettingWrapper sRec : swList) {

                Item__c iTemp = new Item__c();
                iTemp.Lookup__c = iTemp.set1.Lookup__c; 
                iTemp.Setting__c = newSetting.Id; 

        if(!newItemList.isEmpty()) {
                insert newItemList;

        PageReference pageref = new PageReference('/' + newSetting.Id);
        return pageref;

    public class SettingWrapper {

        public Integer index {get;set;}
        public Setting__c set1 {get;set;}



Test Class:

public class SettingClassTest {
    private static testMethod void InsertSettingRecord() {

        List<Setting__c> settingList = new List<Setting__c>();
        List<SettingClass.SettingWrapper> settingWrapper = new List<SettingClass.SettingWrapper>();
        Integer rowToRemove = 0;
        String selectedRole;

        SettingClass sc = new SettingClass(new ApexPages.StandardController(new Setting__c()));


        UserRole urole;
        System.runAs(new User(Id=UserInfo.getUserId()) {
          urole = new UserRole();
          urole.Name = 'New Role';
          insert urole;

        selectedRole = urole.Id;

        List<Lookup__c> lookupList = new List<Lookup__c>();
        Lookup__c lk = new Lookup__c();
        lk.Name = 'Lookup 1';

        insert lookupList;
        System.assertequals(lookupList + '', 'lookupList');


        Setting__c s = new Setting__c();
        s.Name = '1';
        s.Status__c = 'Status 1';
        s.Role_ID__c = selectedRole; 
        s.Role_Name__c = urole.Name;

        insert settingList;   
        System.assertequals(settingList + '', 'settingList');

        PageReference pg = Page.SettingPage;
        ApexPages.StandardController pgCon = new ApexPages.StandardController(ad); 
        SettingClass sClass = new SettingClass(pgCon);

        sClass.rowToRemove = 1;

        SettingClass.SettingWrapper setWrap = new SettingClass.SettingWrapper();

        for(Integer i=0; i<200; i++) {
            Item__c newItem = new Item__c();
            newItem.Lookup__c = lk.Id;
            newItem.Setting__c = s.Id;    
            setWrap.set1 = newItem;
            setWrap.index = 0;

        PageReference pg1 = Page.SettingPage;
        ApexPages.StandardController pgCon1 = new ApexPages.StandardController(ad);
        SettingClass sClass2 = new SettingClass(pgCon1);

        for(SettingClass.SettingWrapper setWrap2 : settingWrapper) {




You can't mix UserRole and other Setup objects with things like CustomObject__c and other non-Setup objects. The way to get around this is mentioned in the documentation; use System.runAs around the Setup objects to avoid the mixed DML error.

For example, the role should be inserted like this:

UserRole urole;
System.runAs(new User(Id=UserInfo.getUserId()) {
    urole = new UserRole();
    urole.Name = 'New Role';
    insert urole;

Be careful, though, as System.runAs uses up a DML operation and DML row each time you call it, so avoid using it more than necessary.

| improve this answer | |
  • Thank you very much for this. Upon saving, error's gone. However, there's this new error System.QueryException: List has no rows for assignment to SObject displaying pertaining to this line sClass2.save(); on the test class and to this line UserRole ur = [SELECT Id, Name FROM UserRole WHERE Id =: selectedRole]; on the controller. Could you also take a look on this? – curdual Mar 16 '18 at 7:59
  • @curdual You hard-coded the ID in Role_Id__c. Change that line so it uses the urRole variable instead. – sfdcfox Mar 16 '18 at 14:37
  • I changed the line and used the urole variabe. But unfortunately, System.QueryException: List has no rows for assignment to SObjecterror still pertains to this line in handler class UserRole ur = [SELECT Id, Name FROM UserRole WHERE Id =: selectedRole]; and also to this line in test class uadc2.save(); – curdual Mar 19 '18 at 2:41
  • @curdual You also did not set the selectedRole variable to the role that you created in your test class. sClass2.selectedRole = uRole.Id; sClass2.save(); – sfdcfox Mar 19 '18 at 2:43
  • I've modified my code and already set the selectedRole variable, but error still exists. Kindly see modified post. – curdual Mar 19 '18 at 3:44

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.