function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AbAb 

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 Ab
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) 
        {
AbAb
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