+ Start a Discussion
SandrineSandrine 

executing test class as specific user

Hello,

I want to execute a test function in test class.

Before executing fucntion i want to create a user with a licence "partner community", how is it posible to achieve ?

thanks for suggestion !
Best Answer chosen by Sandrine
Deepak Pandey 13Deepak Pandey 13
list<User> lstUser =  new list<User>();
      
      Id p = [select id from profile where name='Partner Community'].id;
      Account ac = new Account(name ='Pradip') ;
      insert ac;
      Contact con = new Contact(LastName ='Shukla',AccountId = ac.Id);
      insert con; 
      User U = new User(alias = 'test123', email='test123@noemail.com',
        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
        localesidkey='en_US', profileid = p, country='United States',IsActive =true,
        ContactId = con.Id,
        timezonesidkey='America/Los_Angeles', username='tester@noemail.com');       
       lstUser.add(U);       
      insert lstUser;   
      system.runAs(U) 

All Answers

pkpnairpkpnair
try this 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm
 
Deepak Pandey 13Deepak Pandey 13
list<User> lstUser =  new list<User>();
      
      Id p = [select id from profile where name='Partner Community'].id;

      User U = new User(alias = 'test123', email='test123@noemail.com',
        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
        localesidkey='en_US', profileid = p, country='United States',IsActive =true,
        timezonesidkey='America/Los_Angeles', username='tester@noemail.com');       
       lstUser.add(U);       
      insert lstUser;   
      system.runAs(U) 
        {
SandrineSandrine
list<User> lstUser =  new list<User>();
      
      Id p = [select id from profile where name='Partner Community'].id;

      User U = new User(alias = 'test123', email='test123@noemail.com',
        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
        localesidkey='en_US', profileid = p, country='United States',IsActive =true,
        timezonesidkey='America/Los_Angeles', username='tester@noemail.com');       
       lstUser.add(U);       
      insert lstUser;   
      system.runAs(U) 
        {

I used above code it gives meerror..as the user should be associated with a account and contact
Deepak Pandey 13Deepak Pandey 13
list<User> lstUser =  new list<User>();
      
      Id p = [select id from profile where name='Partner Community'].id;
      Account ac = new Account(name ='Pradip') ;
      insert ac;
      Contact con = new Contact(LastName ='Shukla',AccountId = ac.Id);
      insert con; 
      User U = new User(alias = 'test123', email='test123@noemail.com',
        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
        localesidkey='en_US', profileid = p, country='United States',IsActive =true,
        ContactId = con.Id,
        timezonesidkey='America/Los_Angeles', username='tester@noemail.com');       
       lstUser.add(U);       
      insert lstUser;   
      system.runAs(U) 
This was selected as the best answer