• Karen Darger
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
I trigger that prevents a community user if certain conditions about the user's contact record are true, so in my test class I am running as a community user to insert a FeedItem. 

I followed these instructions for creating and testing as a community user (https://developer.salesforce.com/page/Apex_Testing_with_RunAs).
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? 
I trigger that prevents a community user if certain conditions about the user's contact record are true, so in my test class I am running as a community user to insert a FeedItem. 

I followed these instructions for creating and testing as a community user (https://developer.salesforce.com/page/Apex_Testing_with_RunAs).
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?