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
softectonicsoftectonic 

Testing Partner Portal Account

Hi,

 

In normal operation, Salesforce auto generates the following 3 roles when an Account is enabled as a Partner Account.

- PartnerExecute

- PartnerManager

- PartnerUser

 

I'm trying to enable Partner account via Unit test but I dont think I'm going about it the right way. Below is my code snippet. It failed to generate new partner roles as illustrated in the highlighted below.

 

Please help!

 

    // Create new account. 

      Account testAccount = new Account();

      testAccount.Name = 'My Test Account';

      insert testAccount;  

      

      testAccount = [Select id from Account where id =: testAccount.Id];

      testAccount.IsPartner = true;

      update testAccount;

      

      System.assertEquals(testAccount.IsPartner, true);

         

      //create a new contact

      Contact testContact = new Contact();

      testContact.FirstName = 'Joe';

      testContact.LastName = 'TestUser';

      testContact.ownerid = testUserId;

      testContact.accountid = testAccount.id;

      insert testContact;  

        

       List<UserRole> userRole = [Select Id, Name From UserRole Where PortalAccountId =: testAccount.Id];

       //this assert failed

       System.assertEquals(userRole.size()>0, true);

Best Answer chosen by Admin (Salesforce Developers) 
mikefitzmikefitz

You forgot to create the user record with the associated ContactID and userr ole.

You can't query the userrole table expecting something without any user records.

All Answers

mikefitzmikefitz

You forgot to create the user record with the associated ContactID and userr ole.

You can't query the userrole table expecting something without any user records.

This was selected as the best answer
softectonicsoftectonic

hi mikefitz,

I created the user record just that I didn't include in the code snippet. The full code looks like this:

 

      List<User> users = [SELECT Id FROM User WHERE IsActive = true LIMIT 2];

      Id testUserId1 = users[0].Id; 

      

     // Create new account. 

      Account testAccount = new Account();

      testAccount.Name = 'My Test Account';

      insert testAccount;  

      

      testAccount = [Select id from Account where id =: testAccount.Id];

      testAccount.IsPartner = true;

      update testAccount;

      

      System.assertEquals(testAccount.IsPartner, true);

         

      //create a new contact, setting it's owner to the above user

      Contact testContact = new Contact();

      testContact.FirstName = 'Joe';

      testContact.LastName = 'TestUser';

      testContact.ownerid = testUserId1;

      testContact.accountid = testAccount.id;

      insert testContact;      

        

      List<UserRole> userRole = [Select Id, Name From UserRole Where PortalAccountId =: testAccount.Id];

      System.assertEquals(userRole.size()>0, true);

 

 

softectonicsoftectonic

Thanks mikefitz. the issue was resolved once i created a new portal user with the associated contactid.