I've created a test class and a simple flow below that illustrates the issue with the System user. Two test methods are doing a simple SOQL query on the CollaborationGroup records (one with a Standard User that can access the records, the other is with the System user that cannot access the records). The flow is used to show that the built in method to post to chatter groups is also not working for the System user, but does work for a standard user. The error is misleading, but I believe the problem with the built in flow action is that the System user cannot access the CollaborationGroup records.
@IsTest public class SystemContextTest { private static final string groupName = 'FakeGroup'; private static final string standardUserUsername = '[email protected]'; @TestSetup static void setupData(){ Profile profile = [SELECT Id FROM Profile WHERE Name='Standard User']; User user = new User ( Alias = 'hquinn', Email=standardUserUsername, EmailEncodingKey='UTF-8', LastName='Quinn', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = profile.Id, TimeZoneSidKey='America/Los_Angeles', UserName=standardUserUsername ); insert user; CollaborationGroup cg = new CollaborationGroup(); cg.Name = groupName; cg.CollaborationType = 'Public'; insert cg; } // ----------------------------------------------------- // Utility Methods // ----------------------------------------------------- private static User GetSystemUser() { User user = [SELECT Id, Name FROM User WHERE Name = 'System' LIMIT 1]; system.debug('Verify System User -> ' + user); return user; } private static User GetStandardUser() { User user = [SELECT Id, Name FROM User WHERE UserName = :standardUserUsername LIMIT 1]; system.debug('Verify Standard User -> ' + user); return user; } private static List<CollaborationGroup> GetCollaborationGroup(){ List<CollaborationGroup> cg = [SELECT Name FROM CollaborationGroup WHERE Name = :groupName]; system.debug('Verify CollaborationGroup -> ' + cg); return cg; } private static void PostMessageToChatter(string message) { Map<String, Object> params = new Map<String, Object>(); params.put('Message', message); params.put('GroupName', groupName); Flow.Interview.Call_Post_to_Chatter postToChatter = new Flow.Interview.Call_Post_to_Chatter(params); postToChatter.start(); } private static FeedItem GetFeedItemByBody(string message) { List<FeedItem> feedItems = [SELECT Body, Id FROM FeedItem ORDER BY CreatedDate DESC]; //Can't filter by the Body FeedItem feedItem = null; for(FeedItem fi : feedItems) { if(fi.Body == message) { feedItem = fi; break; } } system.debug('All chatter -> ' + feedItems); system.debug('Matching chatter -> ' + feedItem); return feedItem; } // ----------------------------------------------------- // Get CollaborationGroup Tests // ----------------------------------------------------- static testmethod void GetCollaborationGroupTest_SystemContext() { User user = GetSystemUser(); List<CollaborationGroup> cg; Test.startTest(); System.runAs(user){ cg = GetCollaborationGroup(); } Test.stopTest(); system.assertEquals(groupName, cg[0].Name); } static testmethod void GetCollaborationGroupTest_StandardContext() { User user = GetStandardUser(); List<CollaborationGroup> cg; Test.startTest(); System.runAs(user){ cg = GetCollaborationGroup(); } Test.stopTest(); system.assertEquals(groupName, cg[0].Name); } // ----------------------------------------------------- // Post To Chatter Tests // ----------------------------------------------------- static testmethod void PostChatterFlowActionTest_SystemContext() { User user = GetSystemUser(); string message = 'Using System User.'; Test.startTest(); System.runAs(user) { PostMessageToChatter(message); } Test.stopTest(); FeedItem feedItem = GetFeedItemByBody(message); system.assertEquals(message, feedItem.Body); } static testmethod void PostChatterFlowActionTest_StandardContext() { User user = GetStandardUser(); string message = 'Using standard User.'; Test.startTest(); System.runAs(user) { PostMessageToChatter(message); } Test.stopTest(); FeedItem feedItem = GetFeedItemByBody(message); system.assertEquals(message, feedItem.Body); } } The flow needed for the test class:
<?xml version="1.0" encoding="UTF-8"?> <Flow xmlns="http://soap.sforce.com/2006/04/metadata"> <actionCalls> <name>Post_to_Chatter</name> <label>Post to Chatter</label> <locationX>176</locationX> <locationY>201</locationY> <actionName>chatterPost</actionName> <actionType>chatterPost</actionType> <flowTransactionModel>CurrentTransaction</flowTransactionModel> <inputParameters> <name>text</name> <value> <elementReference>Message</elementReference> </value> </inputParameters> <inputParameters> <name>subjectNameOrId</name> <value> <elementReference>GroupName</elementReference> </value> </inputParameters> <inputParameters> <name>type</name> <value> <elementReference>TargetType</elementReference> </value> </inputParameters> <storeOutputAutomatically>true</storeOutputAutomatically> </actionCalls> <apiVersion>53.0</apiVersion> <constants> <name>TargetType</name> <dataType>String</dataType> <value> <stringValue>Group</stringValue> </value> </constants> <interviewLabel>Call Post to Chatter {!$Flow.CurrentDateTime}</interviewLabel> <label>Call Post to Chatter</label> <processMetadataValues> <name>BuilderType</name> <value> <stringValue>LightningFlowBuilder</stringValue> </value> </processMetadataValues> <processMetadataValues> <name>CanvasMode</name> <value> <stringValue>FREE_FORM_CANVAS</stringValue> </value> </processMetadataValues> <processMetadataValues> <name>OriginBuilderType</name> <value> <stringValue>LightningFlowBuilder</stringValue> </value> </processMetadataValues> <processType>AutoLaunchedFlow</processType> <start> <locationX>50</locationX> <locationY>50</locationY> <connector> <targetReference>Post_to_Chatter</targetReference> </connector> </start> <status>Active</status> <variables> <name>GroupName</name> <dataType>String</dataType> <isCollection>false</isCollection> <isInput>true</isInput> <isOutput>false</isOutput> </variables> <variables> <name>Message</name> <dataType>String</dataType> <isCollection>false</isCollection> <isInput>true</isInput> <isOutput>false</isOutput> </variables> </Flow> The output from running the test class:
=== Test Summary NAME VALUE ─────────────────── ─────────────────────────── Outcome Failed Tests Ran 4 Pass Rate 50% Fail Rate 50% Skip Rate 0% Test Run Id 7070500000y0CFB Test Execution Time 605 ms Org Id 00D050000008pWxEAI Username xxxxx === Test Results TEST NAME OUTCOME MESSAGE RUNTIME (MS) ─────────────────────────────────────────────────────────── ─────── ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ──────────── SystemContextTest.GetCollaborationGroupTest_StandardContext Pass 39 SystemContextTest.GetCollaborationGroupTest_SystemContext Fail System.ListException: List index out of bounds: 0 Class.SystemContextTest.GetCollaborationGroupTest_SystemContext: line 87, column 1 SystemContextTest.PostChatterFlowActionTest_StandardContext Pass 489 SystemContextTest.PostChatterFlowActionTest_SystemContext Fail System.FlowException: Verify the combination of your "Target Name Or ID" and "Target Type" fields to make sure you provide a valid user, Chatter group, or record to post to. Class.Flow.Interview.start: line 51, column 1 Class.SystemContextTest.PostMessageToChatter: line 57, column 1 Class.SystemContextTest.PostChatterFlowActionTest_SystemContext: line 111, column 1