+ Start a Discussion
tmbarrytmbarry 

Need to pull an existing User Record

I am trying to create a test class for a new trigger and it involves a lookup to a user record.  In my test class, do i have to create a new user entry, or is there a way to to reference any existing users in the org for the test? 

Best Answer chosen by Admin (Salesforce Developers) 
Andries.NeyensAndries.Neyens

Besides best practice:

 

If you put this on top of your test class (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm):

 

isTest (seeAllData=true)

 

you can find users as normal:

 

[SELECT Id, Name FROM User Limit 1]

 

and use it as (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm):

 

System.RunAs(theUser) {

 

}

All Answers

Sandeep001Sandeep001

Best practice is to create a new user record in your test class.

Andries.NeyensAndries.Neyens

Besides best practice:

 

If you put this on top of your test class (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm):

 

isTest (seeAllData=true)

 

you can find users as normal:

 

[SELECT Id, Name FROM User Limit 1]

 

and use it as (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm):

 

System.RunAs(theUser) {

 

}

This was selected as the best answer
tmbarrytmbarry

Thanks Andries - I'll give that a try.