You need to sign in to do that
Don't have an account?
Nihar 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.
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.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm
Thanks,
Nihar
All Answers
No, if we need to use System.runAs() we need to first insert the dummy record into database.
Thanks,
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
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
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.
Please let me know what you didn't understand on this.
Thanks,
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.
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
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.
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
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm
Thanks,
Nihar