I have a Test class which was working fine but after a new process builder was created on user object to update a custom field created on user object, it started failing. In my test class code I am inserting the account, contact and then a user related to that contact, the test is throwing Mixed DML error. I understand that this is because I cant do DML on both setup and non-setup in single transaction,so used Test.startTest and Test.stopTest and within it I inserted the user but it didn't work. For the second option ,i.e, User of System.runAs(user), I cant do this as this "user" is what I am inserting . How to deal and resolve this?

  • I have gone through this guide page. But my main issue here is that if I create another future method to create and insert the user, then I need few details from inserted contact which will be present in another testmethod. Secondly, I will have to get the return the user created to another methods for assertion which is also not possible since future method is of void type. – Aditya Apr 7 at 13:10
  • why will you need to return anything just query the data since its already inserted – User6670 Apr 7 at 13:28
  • Yes but I need to run my methods using that user in user context . So I need to call other methods using System.Runas and if I am doing that in the same future method then none of the called method is getting covered . This may be due to this method being future and not test. – Aditya Apr 7 at 13:29
  • 1
    You can do System.runAs(<existing sysadmin account>) – identigral Apr 7 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.