Apex connect api chatter

From PranavNagrecha/AwesomeSalesforceSkills · View interactive version

Apex connect api chatter is an AI agent skill from the GitHub repository PranavNagrecha/AwesomeSalesforceSkills. Use when posting to a Chatter feed, creating feed comments, @mentioning users or groups, or rendering rich-text posts from Apex via the ConnectApi namespace. Trigger keywords: ConnectApi, FeedItem, Chatter post, @mention, rich text post, FeedElementCapabilities, link post. NOT for Experience Cloud f

How to use this skill

Skill file

Apex Connect Api Chatter

Activate this skill when Apex needs to post to Chatter, reply to a feed item, or @mention a user programmatically. The ConnectApi namespace is the only supported way to produce rich-content posts (mentions, links, files, polls) — direct DML on FeedItem is limited and does NOT support mention rendering.


Before Starting

Gather this context before working on anything in this domain:


Core Concepts

ConnectApi vs Direct DML On FeedItem

`FeedItemInput` + `MessageBodyInput` + `MessageSegmentInput`

Rich posts are assembled from segments:

Segments are concatenated in order into a MessageBodyInput, which is wrapped in a FeedItemInput.

Test Context Restrictions

Feed Parent Id Types

Feeds hang off a parent record. Common parent types:


Common Patterns

Plain-Text Post With @Mention

When to use: A trigger or service needs to notify an owner by @mentioning them in a record feed.

How it works:

public static void postMentionToRecord(Id recordId, Id userToMention, String message) {
    ConnectApi.FeedItemInput post = new ConnectApi.FeedItemInput();
    ConnectApi.MessageBodyInput body = new ConnectApi.MessageBodyInput();
    body.messageSegments = new List<ConnectApi.MessageSegmentInput>();

    ConnectApi.MentionSegmentInput mention = new ConnectApi.MentionSegmentInput();
    mention.id = userToMention;
    body.messageSegments.add(mention);

    ConnectApi.TextSegmentInput text = new ConnectApi.TextSegmentInput();
    text.text = ' ' + message;
    body.messageSegments.add(text);

    post.body = body;

    ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), recordId, post);
}

Why not the alternative: insert new FeedItem(Body='@Jane ...') shows literal @Jane text — no notification fires, no link is rendered.

Post With File Attachment

When to use: Uploading a report PDF and posting it to a record feed in one go.

How it works:

ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
ConnectApi.MessageBodyInput body = new ConnectApi.MessageBodyInput();
body.messageSegments = new List<ConnectApi.MessageSegmentInput>{
    new ConnectApi.TextSegmentInput()
};
((ConnectApi.TextSegmentInput) body.messageSegments[0]).text = 'See attached report.';
input.body = body;

ConnectApi.NewFileAttachmentInput file = new ConnectApi.NewFileAttachmentInput();
file.title = 'Weekly Report';
input.capabilities = new ConnectApi.FeedElementCapabilitiesInput();
input.capabilities.content = new ConnectApi.ContentCapabilityInput();
input.capabilities.content.title = file.title;

ConnectApi.BinaryInput binary = new ConnectApi.BinaryInput(
    Blob.valueOf(pdfBytes), 'application/pdf', 'report.pdf');

ConnectApi.ChatterFeeds.postFeedElement(
    Network.getNetworkId(), recordId, input, binary);

Decision Guidance

| Situation | Recommended Approach | Reason |

|---|---|---|

| Plain text post, no mentions | Either ConnectApi or DML | Choose by team convention |

| Text with @mention | ConnectApi MentionSegmentInput | DML produces literal text |

| Post + file attachment | ConnectApi with BinaryInput | No DML equivalent |

| Poll post | ConnectApi PollCapabilityInput | No DML equivalent |

| Scheduled reminder post | Queueable calling ConnectApi | Governor-safe |

| Bulk posts (100s) | Batch + ConnectApi per record | Respects limits per execution |

| Experience Cloud posts | Network.getNetworkId() arg | null posts to internal Chatter |


Recommended Workflow

  1. Confirm Chatter is enabled — feature check via FeatureManagement or an admin confirm.
  2. Identify feed parent — user, group, or record with feed tracking enabled.
  3. Build the FeedItemInput by assembling message segments; handle null/empty text explicitly.
  4. Decide sync vs async — post in the current transaction unless you need isolation; if async, write a governor-bounded Queueable.
  5. Write tests — Test.setMock(ConnectApi.ConnectApi.class, ...) to avoid real feed writes; assert the payload shape.
  6. Test Experience Cloud separately if posts must appear in a partner/customer community — Network.getNetworkId() argument matters.

Review Checklist


Salesforce-Specific Gotchas

  1. Chatter off means ConnectApiException — orgs with Chatter disabled throw on every call; feature-flag the code.
  2. FeedItem DML cannot render mentions — @Jane appears as literal text, no notification fires.
  3. ConnectApi in test requires mocks — SeeAllData=false (the default) blocks real Chatter calls; use Test.setMock or the call throws.
  4. Network Id must be passed — omitting or using the wrong community Id posts to the wrong feed silently.
  5. Mentions require feed visibility — user must have access to the parent record; otherwise the mention is rendered but no notification fires.
  6. Feed tracking must be on — posting to a record whose object lacks feed tracking fails with a specific ConnectApiException.
  7. Polls cannot be edited after posting — only deleted and reposted.
  8. ConnectApi does NOT call triggers on FeedItem — FeedItem triggers fire on DML, but ConnectApi writes go through a separate path; relying on FeedItem triggers for ConnectApi posts is unreliable.

Data 360 ConnectApi data graph reads (Spring '25–Spring '26)

Chatter is the primary ConnectApi surface for this skill, but teams mixing Data 360 data graphs into Apex should know the ConnectApi.CdpQuery additions (check each method's documented API version):

| Method family | API version | Use |

|---|---|---|

| getDataGraphData(..., live) | 63.0 (Spring '25) | Live/real-time data graph reads |

| getDataGraphDataWithLookupKeys(..., noCache) | 64.0 (Summer '25) | Lookup-key reads that bypass cache |

| getMetadataEntities() (+ overloads with dataspace) | 66.0 (Spring '26) | Discover graph entity metadata |

Earlier APIs expose materialized-view reads only. Version-gate call sites on apiVersion, not org release calendar.


Output Artifacts

| Artifact | Description |

|---|---|

| scripts/check_apex_connect_api_chatter.py | Scans for literal @ mentions in FeedItem DML, missing ConnectApi mocks in tests, and unsafe error handling |

| templates/apex-connect-api-chatter-template.md | Work template for assembling FeedItemInput with mentions and attachments |


Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.