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

how to test as self service user

I have a trigger on CaseComment that wants to run certain code only if the comment was posted by a selfserviceuser.


How can I exercise this code in a test class? System.RunAs will not accept a selfserviceuser as argument.


In my opinion, create a self service user and specify the username and all other required fields and try using System.runAs().


Refer the link for more details.


Hope this helps.


I had already tried this before posting my question:



        System.runAs([Select Id from SelfServiceUser
                      Where IsActive = True 
                      Limit 1][0]) {


Compile Error: runAs requires a single argument of type 'User' or 'Version' at line 9 col 9




        User u = [Select Id from SelfServiceUser
                      Where IsActive = True 
                      Limit 1][0];
        System.runAs(u) {


Compile Error: Illegal assignment from SOBJECT:SelfServiceUser to SOBJECT:User at line 10 col 9