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
Michael Johnson 29Michael Johnson 29 

Unit Testing: Set NetworkId of Running User

Is it possible to set the networkid of a running user in a unit test?

I created a test user in a unit test with a profile that only has access to 1 community. Running a test as this user using system.runas(user) still has network.getnetworkId() returning as null. I've tried setting the users networkId both using User.networkId = networkid and User.network.Id = networkid and neither work (invalid foreign key relationship). Is there somewhere in the test I can define what network to run the test under? Any other ideas?
Rishabh Agrawal 18Rishabh Agrawal 18
Try to use User.NetworkScope
Let me know if it works.