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
Roger WickiRoger Wicki 

Create Partner Community User in Test Class

Hey there

Because I want my test class to work without assuming that certain data is present in the deploy-org, I need to create some partner community users in my test class. As I haven't found any guides on how to do that and because I've failed the last time I tried I'd like to ask the developer community for council.

I think the preconditions to create a partner community user in a test class are:
  • Having an account enabled as partner
    • How to enable an account as partner via apex?
  • Having a contact attached to said account
  • Having a partner community profile
    • Necessary, because we use a custom partner community Profile
    • How to create a representation of our real partner community profile?

Thanks for your advice
Roger
Best Answer chosen by Roger Wicki
Grazitti TeamGrazitti Team
Hi Roger Wicki, 

your predictions are correct . below is the code for creating user in the test Method for the Partner Community.

Id p = [select id from profile where name='Partner Community User'].id;
       
        Account ac = new Account(name ='Grazitti') ;
        insert ac; 
       
        Contact con = new Contact(LastName ='testCon',AccountId = ac.Id);
        insert con;  
                  
        User user = 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');
       
        insert user;
        system.runAs(user) {
            // statements to be executed by this test user.
        }

let us know if you have any question .
please don't Forget to Mark this as your best answer if it works fine for you

Regards,
Grazitti Team
Web: www.grazitti.com


All Answers

Grazitti TeamGrazitti Team
Hi Roger Wicki, 

your predictions are correct . below is the code for creating user in the test Method for the Partner Community.

Id p = [select id from profile where name='Partner Community User'].id;
       
        Account ac = new Account(name ='Grazitti') ;
        insert ac; 
       
        Contact con = new Contact(LastName ='testCon',AccountId = ac.Id);
        insert con;  
                  
        User user = 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');
       
        insert user;
        system.runAs(user) {
            // statements to be executed by this test user.
        }

let us know if you have any question .
please don't Forget to Mark this as your best answer if it works fine for you

Regards,
Grazitti Team
Web: www.grazitti.com


This was selected as the best answer
Roger WickiRoger Wicki
Thanks Grazitt Team

There is one additional thing that needs to be in place:
The running user of that piece of code must be assigned a role from Salesforce. I as Admin did not have a Role defined. I fixed that (and added more sample data to required fields for Acc & Contact) and then it worked.
Thanks

Best
Roger
Tad Aalgaard 3Tad Aalgaard 3
How did you add the role?  Code example please.
Roger WickiRoger Wicki
Hi Tad

I mean my system admin user in salesforce didn't have a role assigned. Without having a role, you can't create a partner user. I fixed that for my user in the Salesforce GUI, no additional coding required.
Neel MotaNeel Mota
The code shared by @Grazitti Team will throw mixed dml exceptions