---
schema_version: "1"
id: 1334463b648e11a5d8c980d7ff62c577b570373372b91226912be677e2d6978b
title: Set Up Apex Triggers for Flagging Items
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/experience/264-0-0/networks_moderation_flag_triggers.md
canonical_url: https://help.salesforce.com/s/articleView?id=experience.networks_moderation_flag_triggers.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 685a5273ba15110639b64ecd553d15fb07c51044040de7dfb5e4987b73cf2086
description: Use triggers to create custom advanced moderation logic that automatically flags items in your Experience Cloud site.
products:
  - Experiences
release: "264"
---
# Set Up Apex Triggers for Flagging Items

Use triggers to create custom advanced moderation logic that automatically flags items in your Experience Cloud site.

### Required Editions

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: both Salesforce Classic and Lightning Experience</td></tr><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: <strong>Enterprise</strong>, <strong>Performance</strong>, <strong>Unlimited</strong>, and <strong>Developer</strong> Editions</td></tr></tbody></table>

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small permission">
        <colgroup>
          <col style="width:50%">
          <col style="width:50%">
        </colgroup>
        <thead class="slds-text-title_caps ">
          <tr class="">
            <th scope="colgroup" class="slds-cell-wrap" colspan="2" style="text-align:center;vertical-align:top;" id="d24031e71">User
              Permissions Needed</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d24031e71 ">To create triggers:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d24031e71 ">Modify All Data</td>

          </tr>
        </tbody>
      </table>

![Tip](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-experience-communities-264-0-0-production-enus/bf7df212-91a1-4fa3-a007-4d6f3e3b470b/images/icon_tip.png)

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](https://help.salesforce.com/s/articleView?id=experience.networks_moderator_manage_rules.htm&release=264.0.0&type=5 "Moderation rules help protect your site from spammers, bots, and offensive or inappropriate content. Create and modify rules for your Experience Cloud site to moderate member-generated content. Each rule specifies the member-generated content the rule applies to, the criteria to enforce the rule on, and the moderation action to take.").

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](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-experience-communities-264-0-0-production-enus/bf7df212-91a1-4fa3-a007-4d6f3e3b470b/images/icon_example.png)

Example

This trigger automatically flags posts in your site that contain BadWord.

```java
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.

```java
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);
    }
}
```

#### See Also

- [_SOAP API Developer Guide_](https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/)