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
MandyKoolMandyKool 

Partner User is unable to get Account Information in Test Method

Hi ,

 

We have a code where we are querying the Account and Contact information of partner user.

 

When I use the "Login As Partner" button on contact to login to partner portal, I am able to get Account and Contact Information. And code works perfectly.

 

Now when I am writing the Test Class, I am inserting Account and Contact records.

Then I am using the method system.runAs(partnerUser) method.

The code looks as follows

 

insert accountObj;

 

Contact contactObj = new Contact();

contactObj.AccountId = accountObj.Id;

insert contactObj;

 

partnerUser.ContactId = contactObj.Id;

system.runAs(partnerUser)

{

    //Some code here.

    MyClassName.getPartnerInfo();

}

 

public with sharing class MyClassName

{

    public static PartnerInformation getPartnerInfo()

    {

           Contact contactObj = getContactInfo(UserInfo.getUserId());

           Account accountObj = getAccountInfo(contactObj.Id);

           //Code to create Partner Information record....

    }

}

 

the code fetches the Account Information when i mark my class as "without sharing". If i keep it as "with sharing" it does not fetch the Account Information. Not sure how the same code works when i click on "login as partner". 

 

 

Does anybody have any idea, or someone faced same issue??

 

Thanks,

Mandar.

 

Account account = getAccountInformation();
spraetzspraetz

What type of sharing model do you have?

 

And when logged into the app, does your partner user have access to all accounts and contacts?  or just ones that sharing permits?

MandyKoolMandyKool

The sharing model is private.

 

The partner user will have access to only his "Account" and "Contact".

 

spraetzspraetz

In your test code you're creating your accounts outside of the runAs block, so essentially those accounts and contacts are created by the person running the test (in most cases the admin user)

 

I assume that the partner user you're using in the runAs block does not have the ability to see the records owned by your System Administrator, so when you have a "with sharing" class it will not be able to see those records created in your test.

MandyKoolMandyKool

Yes,

 

Once i use "without sharing" keyword it works.

 

But when we use "Login as partner" button on any of contacts; it works. Just for curiosity, wanted to know how that works.

Also while coding it becomes very difficult to anticipate, which code is going to be used by which user.

 

So for my case, is it the only way to mark my class as "without sharing"?

spraetzspraetz

Once you switch it to "without sharing" sharing is no longer applied when executing that code (even if you're within a runAs block)

 

The reason you're seeing accounts and contacts when using "Login as partner" is because that user can see the accounts and contacts that are available to him as per the sharing model.

 

Some other ways you could make your code work would be to do some manual sharing between the partner user and the records you created, or create the account and contact from inside the runAs block so that those records are available to the partner user when in the runAs context.