Use triggers to create custom advanced moderation logic that automatically flags items in your Experience Cloud site.
Required Editions
| Available in: both Salesforce Classic and Lightning Experience |
| Available in: Enterprise, Performance, Unlimited, and Developer Editions |
| User Permissions Needed | |
|---|---|
| To create triggers: | Modify All Data |
Tip Did you know you can do this in the UI? Most sites don’t need custom
moderation triggers. You can create moderation rules and criteria directly in
Experience Workspaces. For more information, see Create Rules to Moderate Your Experience Cloud Site.
Using triggers to automatically flag items allows you to moderate your Experience Cloud site behind the scenes. These flags are only visible to moderators. You can view flags in Experience Workspaces., query for them in the API, or use custom report types to create reports on flagged items, people whose items are flagged most, and more.
Consider the following when creating triggers:
- Create Apex after insert triggers on FeedItem, FeedComment, ChatterMessage, or ContentDocument.
- Define criteria that when met creates a NetworkModeration (flag) record, with the FeedComment, FeedItem, ChatterMessage, or ContentDocument as the parent.
Example
This trigger automatically flags posts in your site that contain BadWord.
trigger autoflagBadWordOnPost on FeedItem (after insert) {
List<NetworkModeration> flags = new List<NetworkModeration>();
for (FeedItem rec : trigger.new) {
if (!<NetworkId>.equals(rec.networkScope)){
continue;
}
if (rec.body.indexOf('BadWord') >= 0) {
NetworkModeration nm = new NetworkModeration(EntityId = rec.id, Visibility = 'ModeratorsOnly');
flags.add(nm);
}
}
if (!flags.isEmpty()) {
insert(flags);
}
}
A similar trigger on comments would look like this.
trigger autoflagBadWordOnComment on FeedComment (after insert) {
List<NetworkModeration> flags = new List<NetworkModeration>();
for (FeedComment rec : trigger.new) {
if (!<NetworkId>.equals(rec.networkScope)) {
continue;
}
if (rec.commentBody.indexOf('BadWord') >= 0) {
NetworkModeration nm = new NetworkModeration(EntityId = rec.id, Visibility = 'ModeratorsOnly');
flags.add(nm);
}
}
if (!flags.isEmpty()){
insert(flags);
}
}