You need to sign in to do that
Don't have an account?
Karen Darger
Test Class Chatter Post as Community User
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:
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 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?
Option 1
Create 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 2
Create 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
All Answers
Option 1
Create 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 2
Create 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
@pcon, I'm facing a problem related to this and what I need is a method like "createNetwork()" to create a network (community) to use in a test class. I don't have an active network in my organization and I don't want to create one just for testing.
The problem is that I don't know how to create a Network object and insert it into the database. I tried to create a Network object in Apex and insert it but it won't work, as it is a System.Network object. I tried also to create it using the Schema class, like sObject ntw = Schema.getGlobalDescribe().get('Network').newSObject() but it won't work either, because I can't use ntw.put('Name', 'TmpNetwork'), saying that Network.Name is not writable.
Do you know how can I create a Network using Apex, or how I could run tests with ConnectApi, for instance, without having a Network with Live status? Thank you.