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
cjencjen 

Chatter feed trigger validation to only post in a group feed

hi, we have a community and we do not want our commununity users to post to their own feed, rather in a group feed. Reason is because we monitor groups but don't follow individuals so might miss a post. So, i reserached in chatter forums and found this trigger, was told that it will do what we want. 

I am not a coder, so can someone please reveiw the code and let me know if it will do what we want? And, I am having trouble locating out Community ID. I am assuming that NetworkScope is community ID???? so need help finding that too. Lastly, can someone help me with a unit test for this, please?

Thanks very much!!!

Sample Code:

trigger FilterOnChatterPost on FeedItem (before insert) {
for (FeedItem feeditems: trigger.new)
{
if (string.valueOf(feeditems.ParentId).startsWith('005') && feeditems.NetworkScope== '0DBB000000008t2OAA')
{
feeditems.addError('You are only allowed to post the message to Discussion Groups’);
}
}
}
Martijn SchwarzerMartijn Schwarzer
Hi Cjen,

The NetworkScope field has the following description in Salesforce Help (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_feeditem.htm):

Specifies whether this feed item is available in the default community, a specific community, or all communities. This field is available in API version 26.0 and later, if Salesforce Communities is enabled for your organization.

NetworkScope can have the following values:
  • NetworkId—The ID of the community in which the FeedItem is available. If left empty, the feed item is only available in the default community.
  • AllNetworks—The feed item is available in all communities.
Note the following exceptions for NetworkScope:
  • Only feed items with a Group or User parent can set a NetworkId or a null value for NetworkScope.
  • For feed items with a record parent, users can set NetworkScope only to AllNetworks.
  • You can’t filter a FeedItem on the NetworkScope field.

So what the trigger is currently doing is the following:
 
//Before the insert of a FeedItem record
trigger FilterOnChatterPost on FeedItem (before insert) {

  //Loop through all FeedItems in trigger.new (new records to insert) 
  for (FeedItem feeditems: trigger.new){
    //For each FeedItem record:
    //Check for a specific Parent and Networkscope
    if (string.valueOf(feeditems.ParentId).startsWith('005') && feeditems.NetworkScope== '0DBB000000008t2OAA'){
      //Add the error to the feeditem
      feeditems.addError('You are only allowed to post the message to Discussion Groups’);
    }
  }
}

What YOU want to do is create a list of groups (CollaborationGroup) your users MAY post to and check if the FeedItem is for one of those groups. If not, add the error.

A small example of how this could work:
 
//Before the insert of a FeedItem record
trigger FilterOnChatterPost on FeedItem (before insert) {

  List<CollaborationGroup> groups = [Select Id, Name From CollaborationGroup Where Name = 'SampleGroupName1' OR Name = 'SampleGroupName2'];

  Map<Id, CollaborationGroup> allowedGroups = new Map<Id, CollaborationGroup>(groups);

  //Loop through all FeedItems in trigger.new (new records to insert) 
  for (FeedItem feeditem : Trigger.new){

    //For each FeedItem record:
    //Check if the FeedItem is for an allowed group
    //Check if the FeedItem is for the allowed Community

    if (!allowedGroups.containsKey(feeditem) && feeditems.NetworkScope== '0DBB000000008t2OAA'){

      //Add the error to the feeditem
      feeditems.addError('You are only allowed to post the message to Discussion Groups’);
    }

  }
}

The test class would be something like this:
 
@isTest
private class FeedItemTriggerTest { 

	@testSetup
	static void setup(){
		//Create test data: 2 Collaboration groups
		List<CollaborationGroup> groups = new List<CollaborationGroup>();
		CollaborationGroup group1 = new CollaborationGroup(Name = 'AllowedGroup');
		CollaborationGroup group2 = new CollaborationGroup(Name = 'NotAllowedGroup');

		groups.add(group1);
		groups.add(group2);

		insert groups;
	}

	static testmethod void testNewFeedItemForAllowedGroup(){
		List<CollaborationGroup> groups = [Select Id, Name from CollaborationGroup Where Name = 'AllowedGroup'];
		System.assertEquals(1, groups.size(), 'There should be 1 allowed group');

		CollaborationGroup allowedGroup = groups.get(0);

		Test.startTest();

		FeedItem item = new FeedItem();
		item.Body = 'My first post to an allowed group';
		item.ParentId = allowedGroup.Id;

		insert item;
		Test.stopTest();

		System.assertNotEquals(null, item.Id, 'The record should be added to the Database');
	}

	static testmethod void testNewFeedItemForNotAllowedGroup(){
		List<CollaborationGroup> groups = [Select Id, Name from CollaborationGroup Where Name = 'NotAllowedGroup'];
		System.assertEquals(1, groups.size(), 'There should be 1 not allowed group');

		CollaborationGroup notAllowedGroup = groups.get(0);

		Test.startTest();

		FeedItem item = new FeedItem();
		item.Body = 'My first post to a not allowed group';
		item.ParentId = notAllowedGroup.Id;

		Boolean hasError = false;

		try{
		insert item;
		}catch(DMLException e){
			hasError = true;
		}

		Test.stopTest();

		System.assertEquals(null, item.Id, 'The record should not be added to the Database');
		System.assertEquals(true, hasError, 'There should have been an error');
	}
}

Of course you will have to change a few things to make this work for your situation, but the basics should be covered now.

I hope this helps!

Happy coding and best regards,
Martijn Schwärzer