0

I'm receiveing an error. I have tried using the fix system.RunAs but still it is failing.

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): MPM4_BASE__Milestone1_Resource__c, original object: User: []

@isTest
private class NewActionItemControllerTest {
    static NewActionItemController apexController;
    static ApexPages.StandardController controller;
    static PageReference pageRef;
    static User testUser;
    //static Visit__c visit;
    static Visit__c visitTouchpointTemplate;
    static Hotel_Owner__c ho;
    static Management_Company__c mc;
    static List<Action_Item__c> listActionItem;
    static List<Visit__c> listVisit;
    static Touchpoint_Participant__c tpp1;
    static Contact cont1;
    static Account account;
    static Attachment attachment;
    static Participating_Hotel__c participatingHotels;

    static {
        Profile pAdmin = [select Id from Profile where name = 'System Administrator'];        

        UserRole ur = new UserRole(name='Director Sales & Marketing');
        insert ur;

        // create a system admin test user
        testUser = new User(email='[email protected]', lastname = 'test', profileId = pAdmin.Id, username='[email protected]', UserRoleId = ur.Id, 
                            alias = 'test1', localesidkey='en_US', timezonesidkey='America/Los_Angeles', emailencodingkey='UTF-8', languagelocalekey='en_US',
                            Functional_Area__c='Sales and Marketing Specialist');
        insert testUser;
    }

    public static void load() {
         /*
          * UPDATE:  12/18/15
          * @slalom: Updated to use 'Mass Deployment Functional Area Mapping' custom setting instead of 'Role and Functional Area Mapping'.
          */
        Mass_Deployment_Functional_Area_Mapping__c focusSetting1 = new Mass_Deployment_Functional_Area_Mapping__c();
        focusSetting1.Name = 'Sales and Marketing Specialist';
        //focusSetting1.Functional_Area_Director_Role__c = 'Director Sales & Marketing';
        //focusSetting1.Functional_Area_Manager_Role__c = 'Sales & Marketing Manager';
        focusSetting1.Contact_Title__c = 'Design & Construction';

        insert focusSetting1;

        ho = new Hotel_Owner__c(Name='Test Hotel Owner', Hotel_Owner_ID__c = 12345.0);
        insert ho;

        mc = new Management_Company__c(Name='Test Hotel Owner', Management_Company_ID__c= 54321.0);
        insert mc;

        account = new Account(Name='Test Account', Owner__c = ho.Id, Management_Company__c = mc.Id);
        insert account;

        cont1 = new Contact(FirstName='Contact FName', LastName='Contact LName', Title='Design & Construction', AccountId=account.Id, 
                                    Email='[email protected]', Hotel_Owner__c = ho.Id);
        insert cont1;

        //visit = new Visit__c(Account__c = account.Id, Visit_Date__c = System.today(), Visit_Types__c = 'Initiative', Visit_Description__c='abcd', Visit_Summary_Details__c='Hello Test');
        //insert visit;

        listVisit = new List<Visit__c>();

        Visit__c v1 = new Visit__c(Account__c = account.Id, Visit_Date__c = System.today(), Visit_Description__c='abcd', Visit_Summary_Details__c='Hello Test', Interaction_Type__c = 'Hotel Onsite');
        listVisit.add(v1);

        Visit__c v2 = new Visit__c(Management_Company__c = mc.Id, Visit_Date__c = System.today(), Visit_Description__c='abcd', Visit_Summary_Details__c='Hello Test', Interaction_Type__c = 'Portfolio Review');
        listVisit.add(v2);

        Visit__c v3 = new Visit__c( Visit_Date__c = System.today(), Visit_Description__c='abcd', Visit_Summary_Details__c='Hello Test', Interaction_Type__c = 'Group Deployment');
        listVisit.add(v3);

        Visit__c v4 = new Visit__c(Management_Company__c = mc.Id, Visit_Date__c = System.today(), Visit_Description__c='abcd', Visit_Summary_Details__c='Hello Test', Interaction_Type__c = 'Portfolio Review');
        listVisit.add(v4);

        Visit__c v5 = new Visit__c( Visit_Date__c = System.today(), Visit_Description__c='abcd', Visit_Summary_Details__c='Hello Test', Interaction_Type__c = 'Group Deployment');
        listVisit.add(v5);

    Visit__c vOwner = new Visit__c(Owner__c = ho.Id, Visit_Date__c = System.today(), Visit_Description__c='abcd', Visit_Summary_Details__c='Hello Test', Interaction_Type__c = 'Hotel Onsite');
        listVisit.add(vOwner);

    Visit__c vContact = new Visit__c(Account__c = account.Id, PC_GM__c=cont1.Id, Visit_Date__c = System.today(), Visit_Description__c='abcd', Visit_Summary_Details__c='Hello Test', Interaction_Type__c = 'Hotel Onsite');
        listVisit.add(vContact);

        insert listVisit;

        //Insert Participating Hotels for Group Deployment Touchpoint
        Participating_Hotel__c ph = new Participating_Hotel__c(Hotel__c = account.Id, Touchpoint__c = listVisit.get(2).Id);
        insert ph;

        listActionItem = new List<Action_Item__c>();
        Action_Item__c ai1 = new Action_Item__c(Visit__c = v1.Id, Status__c = 'New', Action_Item_Name__c='Test', Additional_Context__c = 'Test', 
                            Specialist__c = testUser.Id, Action_Item_Type__c = FocusToolConstant.AIT_DELEGATED, Approver_User__c = testUser.Id,  Request_Reason__c= 'Portfolio Review', 
                            Hotel__c = v1.Account__c, Functional_Area_Assign_To__c = 'Sales and Marketing Specialist', Touchpoint_Timeline__c = 'High',
                            Due_Date__c = System.today(), Winning_Metric_List__c='Guest Reviews', Prior_Actions__c = 'testing');
        listActionItem.add(ai1);

        Action_Item__c ai2 = new Action_Item__c(Visit__c = v1.Id, Status__c = 'New', Action_Item_Name__c='Test', Additional_Context__c = 'Test', 
                            Specialist__c = testUser.Id, Action_Item_Type__c = FocusToolConstant.AIT_NON_DELEGATED, Approver_User__c = testUser.Id,  Request_Reason__c= 'Portfolio Review', 
                            Hotel__c = v1.Account__c, Functional_Area_Assign_To__c = 'Sales and Marketing Specialist', Touchpoint_Timeline__c = 'High',
                            Due_Date__c = System.today(), Winning_Metric_List__c='Guest Reviews');
        listActionItem.add(ai2);

        Action_Item__c ai3 = new Action_Item__c(Visit__c = v1.Id, Status__c = 'New', Action_Item_Name__c='Test', Additional_Context__c = 'Test', 
                            Specialist__c = testUser.Id, Action_Item_Type__c = FocusToolConstant.AIT_MYACTION, Approver_User__c = testUser.Id,  Request_Reason__c= 'Portfolio Review', 
                            Hotel__c = v1.Account__c, Functional_Area_Assign_To__c = 'Sales and Marketing Specialist', Touchpoint_Timeline__c = 'High',
                            Due_Date__c = System.today(), Winning_Metric_List__c='Guest Reviews', IHG_Internal_Comments__c = 'testing for my action');
        listActionItem.add(ai3);

        Action_Item__c ai4 = new Action_Item__c(Visit__c = v3.Id, Status__c = 'New', Action_Item_Name__c='Test', Additional_Context__c = 'Test', 
                            Specialist__c = testUser.Id, Action_Item_Type__c = FocusToolConstant.AIT_GROUPDEPLOYMENT, Approver_User__c = testUser.Id,  Request_Reason__c= 'Portfolio Review', 
                            Hotel__c = v3.Account__c, Functional_Area_Assign_To__c = 'Sales and Marketing Specialist', Touchpoint_Timeline__c = 'High',
                            Due_Date__c = System.today(), Winning_Metric_List__c='Guest Reviews', Prior_Actions__c = 'testing');
        listActionItem.add(ai4);

        Action_Item__c ai5 = new Action_Item__c(Visit__c = v2.Id, Status__c = 'New', Action_Item_Name__c='Test', Additional_Context__c = 'Test', 
                            Specialist__c = testUser.Id, Action_Item_Type__c = FocusToolConstant.AIT_PORTFOLIORREVIEW, Approver_User__c = testUser.Id,  Request_Reason__c= 'Portfolio Review', 
                            Hotel__c = v2.Account__c, Functional_Area_MultiSelect__c ='Sales and Marketing Specialist;Revenue Specialist', Touchpoint_Timeline__c = 'High',
                            Due_Date__c = System.today(), Winning_Metric_List__c='Guest Reviews');
        listActionItem.add(ai5);

        insert listActionItem;

        tpp1 = new Touchpoint_Participant__c(User__c = testUser.Id, Touchpoint__c = listVisit.get(0).Id, Participant_Position__c='Lead', Attended_Meeting__c=true);
        insert tpp1;

        participatingHotels = new Participating_Hotel__c (Touchpoint__c = listVisit.get(2).Id, Hotel__c = account.Id);
        insert participatingHotels;
    }

    /*
     * This test method test the new action item creation flow for all types of action item. Also covers the UI events for action item type change.
    */
    @isTest static void checkInsertActionItemFlow() {
      Test.startTest();
      System.runAs(testUser) {
        load();
          controller = new ApexPages.StandardController(new Action_Item__c(Visit__c = listVisit.get(0).Id));
          apexController = new NewActionItemController(controller);

        //Test Hotel Onwer Action Item Create
        //Validate Missing Required Fields
        apexController.saveActionItem(); 
        apexController.actionItem = listActionItem.get(1);
          for(NewActionItemController.AccountWrapper aiw: apexController.listAccount) {
                aiw.isChecked = true;
            }
        apexController.saveActionItem();

        //Test DML Exception
        apexController.actionItem.Action_Item_Type__c = FocusToolConstant.AIT_PORTFOLIORREVIEW;
        apexController.actionItem.Functional_Area_MultiSelect__c ='FPS Lead;Sales and Marketing Specialist;Revenue Specialist';
        apexController.saveActionItem();

        //Test My Action Item Create
        apexController.actionItem.Action_Item_Type__c = FocusToolConstant.AIT_MYACTION;
        apexController.changeActionType();
        //Validate Missing Required Fields
        apexController.saveActionItem();
        //Validate and Insert with Valid Data
        apexController.actionItem = listActionItem.get(2);
          for(NewActionItemController.AccountWrapper aiw: apexController.listAccount) {
                aiw.isChecked = true;
            }
        apexController.saveActionItem();

        //Test Specialist Action Item Create
        apexController.actionItem.Action_Item_Type__c = FocusToolConstant.AIT_DELEGATED;
        apexController.changeActionType();
        //Validate Missing Required Fields
        apexController.saveActionItem();
        //Validate and Insert with Valid Data
        apexController.actionItem = listActionItem.get(0);
          for(NewActionItemController.AccountWrapper aiw: apexController.listAccount) {
                aiw.isChecked = true;
            }
        apexController.saveActionItem();

      //Test Portfolio Review Action Item Create
      controller = new ApexPages.StandardController(new Action_Item__c(Visit__c = listVisit.get(3).Id));
          apexController = new NewActionItemController(controller);
        //Validate Missing Required Fields
        apexController.saveActionItem();
        //Validate and Insert with Valid Data
        apexController.actionItem = listActionItem.get(4);
        apexController.saveActionItem();

        //Test Group Deployment Action Item Create
        controller = new ApexPages.StandardController(new Action_Item__c(Visit__c = listVisit.get(4).Id));
          apexController = new NewActionItemController(controller);
        //Validate Missing Required Fields
        apexController.saveActionItem();
        //Validate and Insert with Valid Data
        apexController.actionItem = listActionItem.get(3);
          for(NewActionItemController.AccountWrapper aiw: apexController.listAccount) {
                aiw.isChecked = true;
            }
        apexController.saveActionItem();

        apexController.saveAndNewActionItem();

        //Test Specialist Action Item Owner Visit Create
        controller = new ApexPages.StandardController(new Action_Item__c(Visit__c = listVisit.get(5).Id, Action_Item_Type__c = FocusToolConstant.AIT_DELEGATED));
            apexController = new NewActionItemController(controller);
            apexController.fetchUsers();

        //Test Specialist Action Item PC/GM Visit Create
        controller = new ApexPages.StandardController(new Action_Item__c(Visit__c = listVisit.get(6).Id, Action_Item_Type__c = FocusToolConstant.AIT_DELEGATED));
            apexController = new NewActionItemController(controller);
            apexController.fetchUsers();

            //Test Exception
            apexController.visit = null;
        apexController.saveActionItem();
      }
      Test.stopTest();
}

1 Answer 1

4

Bigger problem: Every test methods must have asserts to validate the expected functionality.

Solution to mentioned one- You do not have to insert a user record to use System.RunAs, Code snippet from documentation:

@isTest
private class TestRunAs {
   public static testMethod void testRunAs() {
        // Setup test data
        // Create a unique UserName
        String uniqueUserName = 'standarduser' + DateTime.now().getTime() + 
            '@testorg.com';
        // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        User u = new User(Alias = 'standt', Email='[email protected]',
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles',
         UserName=uniqueUserName);

        System.runAs(u) {
              // The following code runs as user 'u'
              System.debug('Current User: ' + UserInfo.getUserName());
              System.debug('Current Profile: ' + UserInfo.getProfileId());
          }
    }
}

Read more about Mixed DML Operations in Test Methods.

You must log in to answer this question.

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