
Here is my code that uses the getPortalUser method from above to create the portal user, then insert the post as the user:
User runningUser = getPortalUser(PortalType.CSPLitePortal, null, true);
Test.StartTest();
system.runas(runningUser){
FeedItem post = new FeedItem();
post.ParentId = userinfo.getuserid();
post.Body = 'test post';
post.NetworkScope=Network.getNetworkId();
insert post;
}
I get this error when inserting the post: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id
I know that I need to set the NetworkScope of the post, as it describes in this article (https://help.salesforce.com/apex/HTViewSolution?id=000187667&language=en_US), but Network.getNetworkID() is null. How can I retrieve this information in a test class?5 Antworten
Understood. The problem is that your getPortalUser method does not get a user that is associated with a network. What I would suggest is that you do one of the following:Option 1Create a new method that called getNetworkPortalUser this queries the NetworkMember [1] object and finds a MemberId that meets the same criteria as your getPortalUser method does. You can use the Member.Username / Member.Profile.Name lookups to aid in this. Then once you find a User that meets the criteria and is in a Network, query them with the same field query you use in getPortalUser but directly against the known MemberId. Then use that User in your test above.Option 2Create a few new methods that create all the data you need. You'll need to create createNetwork, createPortalUser, and createNetworkMember methods that create a new Network, a new User and a new link between them. Then use that User in your test.Option 1 is going to be faster but could be more error prone since you do not have control over every facet. Option 2 is slower and slightly harder to do, but it allows you to have much more granular control over the data you are using. Plus it allows you to know all of the expected Id (such as NetworkScope) without having to rely on one already existing.[1] https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_networkmember.htm