-1

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

Codes:

@isTest
public class ClosingReportHandlerAsiaHelper_TEST {

    @isTest
    public static void test(){
        BRA_Branch__c testBranch= new BRA_Branch__c();
        testBranch.Name='Test Branch';
        testBranch.Branch_Country_Code_PL__c='MY';
        insert testBranch;

        User hkUser = new User();
        hkUser = insertHkUser();

        List<Profile> testProfile = [select Id,name from Profile where name ='System Administrator' limit 1]; 
        User testUser;
        if(testProfile.isEmpty()){
            testUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
            testUser.Bypass_Validation__c = True;
            //testUser.UserRole = ur;
            Update testUser;
        }
        else{
            testUser = new User( 
                //UserRole = [Select Name from UserRole where Name = 'CEO' limit 1],
                profileId = testProfile[0].id, 
                username = 'newUser34242342@23424yahoo.com', 
                email = 'pb23424@ff.com',  
                emailencodingkey = 'UTF-8', 
                localesidkey = 'en_US', 
                languagelocalekey = 'en_US', 
                timezonesidkey = 'America/Los_Angeles', 
                alias='nuser', 
                lastname='lastname', 
                Bypass_Validation__c = True,
                CountryCode = 'MY'
            );
            insert testUser;
        }
        System.runAs(testUser){

            SSOC_Code__c newSsoc = new SSOC_Code__c();
            newSsoc.Name = 'Legislator';
            newSsoc.Code__c = '11110';
            Database.insert(newSsoc);

            Account testAccount = TestUtils.createAccount('CR Candidate Pool Test');
            testAccount.BillingCity = 'Malaysia';
            testAccount.BillingState = 'California';
            testAccount.BillingStateCode = 'CA';
            testAccount.BillingStreet = 'Street';
            testAccount.BillingPostalCode = '452817';
            testAccount.Validated_Account__c = true;
            testAccount.Payment_Term__c = '21 Days';
            testAccount.Approved_Credit_Limit__c = 100000000;
            testAccount.CreatorCountry__c = 'MY';
            insert testAccount;

            BRT_BraTerritory__c newBranchAcc = new BRT_BraTerritory__c();
            newBranchAcc = TestUtils.createBranchTerritory(newBranchAcc);
            newBranchAcc.Account__c = testAccount.Id;      
            newBranchAcc.Branch__c = testBranch.Id;
            insert newBranchAcc;

            Contact testContact = TestUtils.createContact(new Contact (AccountId = testAccount.id, FirstName = 'Test1', LastName = 'Conatct1', Email = 'admin=talentrover.com@example.com', Title='Mr'));
            insert testContact;

            Contact objClient = TestUtils.createContact(new Contact (AccountId = testAccount.id, FirstName = 'ClientTest1', LastName = 'ClientConatct1', Email = 'Clientadmin=talentrover.com@example.com',Title='Mr'));
            insert objClient;

            TR1__Job__c testJob = new TR1__Job__c();
            testJob.Name = 'Test1';
            testJob.TR1__Number_of_Openings__c = 10;
            testJob.TR1__Years_of_Experience__c = 5 ;
            testJob.TR1__Years_of_Experience__c  =  3;
            testJob.TR1__Salary_Low__c = 154500 ;
            testJob.TR1__Salary_High__c = 1000 ;
            testJob.TR1__Estimated_Start_Date__c = Date.today() ;
            testJob.TR1__Estimated_End_Date__c = Date.today().addDays(10);
            testJob.TR1__Client_Job_Description__c = 'Test';
            testJob.TR1__Client_Description__c = 'Test';
            testJob.TR1__Open_Date__c = Date.today();
            testJob.TR1__Closed_Date__c =Date.today().addDays(10);
            testJob.TR1__City__c = 'Test1';
            testJob.TR1__Bonus__c = 'test';
            testJob.TR1__Division__c = 'test';
            testJob.TR1__Account__c = testAccount.Id;
            testJob.TR1__External_Job_Title__c = 'Test External Job';
            testJob.TR1__Status__c = 'Abierto';
            testJob.SSOC__c = newSsoc.id;
            testJob.TECH_BRT_Lookup__c = newBranchAcc.Id;
            testJob.CreatorCountry__c = 'MY';
            insert testJob;

            list<Attachment> tempAttachmentList = [Select Name , parentId from Attachment where parentId = :testJob.Id];
            if(tempAttachmentList.size()>0)
                delete tempAttachmentList; 

            List<TR1__Closing_Report__c>  closingReportList=new List<TR1__Closing_Report__c>();
            id crRecordTypeIdRetained = Schema.SObjectType.TR1__Closing_Report__c.getRecordTypeInfosByName().get('Retained').getRecordTypeId();
            id crRecordTypeIdPermanent = Schema.SObjectType.TR1__Closing_Report__c.getRecordTypeInfosByName().get('Permanent').getRecordTypeId();
            TR1__Closing_Report__c testCR = new TR1__Closing_Report__c(
                RecordTypeId = crRecordTypeIdRetained,
                TR1__Salary__c = 100,
                TR1__Fee_Percentage__c = 10,
                TR1__Start_Date__c  = date.today(),              
                TR1__Booking_Date__c = date.today(),
                TR1__Billing_Street__c = 'test street',
                TR1__Billing_City__c  = 'test city',
                TR1__Billing_State__c = 'test state',
                TR1__Billing_Zip_Code__c = '21333',
                TR1__Job_Percentage__c  =100, 
                TR1__Job__c = testJob.Id, 
                TR1__Account__c = testAccount.Id, 
                TR1__Person_Placed__c =  testContact.Id
            );
            closingReportList.add(testCR);

            Test.startTest();
            insert closingReportList;

            testJob.TR1__Contact__c = testContact.Id;
            //List<TR1__Closing_Report__c> testCRList = [Select Id,Sync_Error_Url__c,Sync_Error_Message__c,Sync_Status__c from TR1__Closing_Report__c where Id =:testCR3.Id];
            List<String> pickListValue = Label.ClosingReportTriggerHandlerPicklistValue.split(':');
            //   system.assertEquals(testCRList[0].Sync_Status__c, pickListValue[0]);
            system.assertNotEquals(null,testJob.id);
            Test.stopTest();
        }
    }
  • Please include the relevant sections of code from the class being tested. See How to Ask for how to improve your question so that it is likely to get an answer. – Dave Humm Nov 15 '18 at 12:03
0

There are several Q&A where the same thing has been discussed that we get the mixed DML when we try updating the setup(User object here) and non setup objects in the same context.

Now coming to how to resolve is as mentioned with example in SF documentation: https://developer.salesforce.com/docs/atlas.en-us.198.0.apexcode.meta/apexcode/apex_dml_non_mix_sobjects_test_methods.htm

That is, query the user first and then use System.RunAs()

  • @user62047 Were you able to resolve the issue? – Khushboo Dua Nov 16 '18 at 8:15

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.