+ Start a Discussion
Nihar ANihar A 

System.runAs method

Can we use system.runAs() with the user record that is created but not inserted into database?

For Example :

User A = new User();
A.name = XXXXX
A.Profile = XXXXX
A.role = XXXXX
A.email = XXXX

System.runAs(A){

//some code

}

In the above exaample I am creating a new Dummy test user A , providing some field values but not inserting it in the database.

If we can use it what are the pros and cons of using it this way.

Thank you.
Best Answer chosen by Nihar A
Nihar ANihar A
Lines from Technical Documentation "The runAs method implicitly inserts the user that is passed in as parameter if the user has been instantiated, but not inserted yet." 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm

Thanks,
Nihar

All Answers

GauravGargGauravGarg
Hi Nihar,

No, if we need to use System.runAs() we need to first insert the dummy record into database. 

Thanks,
Gaurav
Nihar ANihar A
Hi gaurav,

Thanks for the response. I have tried just creating a test class like in the above example and it did not throw any error while running the test class. Any insights on that?

Thank you
GauravGargGauravGarg
Is test class covering you required test scenario's?

 
Nihar ANihar A
I hav e not written the test class for any APex controller or Trigger. I have just written it to see if it throws any error if we execute it without inserting it.
GauravGargGauravGarg

Hi Nihar,

Test Classes are always written to validate the Apex class and trigger scenario's, without implementing test class to cover either apex class or trigger. 

Test class is of no use, please try to write test class for some class and trigger, and then then try again the same scenario. 

Thanks,

Gaurav

Nihar ANihar A
Sure.. I have tried that way to see if it gives compile time or run time error.
Nihar ANihar A
Hi gaurav,

I Found an interesting thing related to this. Please let me know your insights on this.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm

Nihar.
GauravGargGauravGarg
Hi Nihar,

Please let me know what you didn't understand on this. 

Thanks,
Gaurav
Nihar ANihar A
Hello Gaurav,

In the example provided in the documentation, the user that is created is not being inserted into database and it is being used to run a code block.

Nihar.
GauravGargGauravGarg
Hi Nihar,

ya look like salesforce made some updates, even without inserting user into test data we could use this system.runAs().

Thanks for enlightening me. 

Thanks,
Gaurav
Nihar ANihar A
No Problem Gaurav.. 

I have posted the question because even I was confused looking at the example. But I think we should be careful beforing considering using in that way I guess. I am not sure about the pros of cons of using it that way.

Nihar.
GauravGargGauravGarg

Hi Nihar,

If your query been resolved can you please select one of the answer as best answer and mark this question as solved.

Thanks,

Gaurav

Nihar ANihar A
Lines from Technical Documentation "The runAs method implicitly inserts the user that is passed in as parameter if the user has been instantiated, but not inserted yet." 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm

Thanks,
Nihar
This was selected as the best answer