Post a Chatter Feed Item using the Salesforce Apex REST Data Destination

This recipe shows you how to configure a Data DestinationClosed A Data Destination specifies where to send data from a submitted form. You can use Data Destinations to automate data sharing and storage, routing data to a specific service (such as email or cloud storage) in several different formats. to post a Salesforce Chatter Feed Item when a user submits a form. This recipe describes how to:

  1. Configure a Salesforce Apex REST Data Destination to post a Chatter Feed Item.

  2. Configure a Document with a Request Body to post a Chatter Feed Item.

  3. Link the Data Destination and Document to the form.

Info:We’re now TrueContext.

Available on the Advanced and Enterprise tiers:

Essentials
Advanced
Enterprise

Configure a Salesforce Apex REST Data Destination to post a Chatter Feed Item

Prerequisites

  • Your TrueContext team must be on the Advanced or Enterprise tier.

  • You must be an Admin user.

  • You must have a Salesforce account with Chatter enabled. Chatter is enabled by default on Salesforce organizations created after June 22, 2010.

  • You must have a Salesforce Connection configured.

    Info:The topic Salesforce Connection describes in detail how to configure a Salesforce Connection.

Steps

  1. Go to Forms & Integrations > Data Destinations.

  2. Select CREATE DATA DESTINATION, and then select Salesforce Apex REST.

  3. Configure the Destination Basics, Filtering, and Response Configuration tabs as needed.

    Info:The topic Salesforce Apex Data Destination describes in detail how to configure a Salesforce Apex RESTClosed The Salesforce Apex REST is a type of Salesforce Data Destination that enables TrueContext to interact with Salesforce by means of an Apex REST API endpoint. If using an Apex REST API endpoint, developers can write APEX code to send custom messages that pull specific data from submitted forms. Data Destination.

  4. On the Connection tab:

    1. Select your Salesforce Connection.

    2. Select POST as the HTTP Method.

    3. Under Apex REST Prefix, select Custom, and enter the following as the prefix: /services/data/v51.0

      Note:Make sure to adjust the API version to your current Salesforce API version. In the example, the API version is 51.0.

    4. Enter the following as the Apex REST Endpoint: /chatter/feed-elements

    The following example shows the configuration of the Connection tab.

    Tip:You can leave the custom prefix field blank, and enter the prefix and the endpoint in the Apex REST Endpoint section.

  5. Select Create.

    Result: You created a Salesforce Apex REST Data Destination that can post a Chatter Feed Item.

Configure a Document with a Request Body to post a Chatter Feed Item

Prerequisites

  • Make note of the Salesforce Record ID, Group ID, or User ID that will parent the Feed Item. You must include one of the IDs in the Salesforce SubjectId parameter.

  • You must have a Request Body formatted according to Salesforce requirements.

    Info:The Salesforce Developer Guide explains in detail how to format a Request body to Post a Chatter Feed item.

Steps

  1. Go to Forms & Integrations > Documents.

  2. Select CREATE DOCUMENT.

  3. Select a document type from the Template-driven Text Documents. You can select DRELClosed Data Reference Expression Language (DREL) is used to get form data and metadata and add it to a string, such as dates, usernames, or answers to questions in forms., FreeMarker, or Handlebars.

  4. Configure your document as needed.

    Info:The following topics describe in detail how to configure each document type:
  5. Include the Request Body formatted according to Salesforce requirements in the following sections:

    • DREL documents—in the DREL Template section.

    • FreeMarker documents—upload an FTL file in the FreeMarker Template section.

    • Handlebars documents—in the Handlebars Template section.

    Info:We recommend that you escape the data for all JSON, XML, HTML, and CSV documents. This ensures that the system generates a valid document in your selected format.

    The following example shows a DREL Document with a Request Body formatted according to Salesforce requirements.

    The File Extension for this example is .json. The DREL Template section contains the Request Body.

    Info:The following sections contain Request Body examples.
  6. Select Create.

    Result: You created a Document with a Request Body to post a Salesforce Chatter Feed Item.

DREL Request Body examples

The following Request Body posts a message to Chatter based on the answer to a question in a TrueContext form, where

Note:All DREL expressions in the example include a property to escape the data. This means that if the answer has special characters or line breaks, the system still generates a valid JSON document.

{
  "body": {
    "messageSegments": [
      {
        "type": "Text",
        "text": "%a[Message][#escapeJson] - Posted from TrueContext"
      }
    ]
  },
  "feedElementType": "FeedItem",
  "subjectId": "%a[recordID][#escapeJson]"
}

In this next example,

  • a question with the Unique ID SiteID populates the Salesforce entityId parameter

  • a question with the Unique ID WOLocation populates part of the text in the Chatter Feed Item

  • a question with the Unique ID SFDCUserID populates a Salesforce User ID that displays as a Mention in the Chatter message, and

  • the Salesforce subjectID parameter is hardcoded. This means that the subjectId is the same for every submitted form.

Note:All DREL expressions in the example include a property to escape the data. This means that if the answer has special characters or line breaks, the system still generates a valid JSON document.

{
  "body": {
    "messageSegments": [
      {
        "type": "Text",
        "text": "A site inspection was submitted for location "
      },
      {
        "type": "EntityLink",
        "entityId": "%a[SiteID][#escapeJson]"
      },
      {
        "type": "Text",
        "text": " at %a[WOLocation][#escapeJson] by "
      },
      {
        "type": "Mention",
        "id": "%a[SFDCUserID][#escapeJson]"
      }
    ]
  },
  "feedElementType": "FeedItem",
  "subjectId": "0F91I000000hndjSAA"
}

In this final DREL example,

  • a question with the Unique ID AccountID, and a question with the Unique ID SiteID populate the Salesforce entityId parameter

  • a question with the Unique ID SFDCUserID populates a Salesforce User ID that displays as a Mention in the Chatter message, and

  • the Salesforce subjectID parameter is hardcoded. This means that the subjectId is the same for every submitted form.

Note:All DREL expressions in the example include a property to escape the data. This means that if the answer has special characters or line breaks, the system still generates a valid JSON document.

{
  "body": {
    "messageSegments": [
      {
        "type": "Text",
        "text": "A new opportunity was was created for account "
      },
      {
        "type": "EntityLink",
        "entityId": "%a[AccountID][#escapeJson]"
      },
      {
        "type": "Text",
        "text": " from an inspection at location "
      },
      {
        "type": "EntityLink",
        "entityId": "%a[SiteID][#escapeJson]"
      },
      {
        "type": "Text",
        "text": " by "
      },
      {
        "type": "Mention",
        "id": "%a[SFDCUserID][#escapeJson]"
      },
      {
        "type": "Hashtag",
        "tag": "NewSalesOpp"
      }
    ]
  },
  "feedElementType": "FeedItem",
  "subjectId": "0F91I000000hndjSAA"
}

Handlebars Request Body example

In the following example,

  • the expression {{pf:outputFormat "JSON"}} sets the output format to JSON. When you add this expression, the system escapes the data referenced by Handlebars and generates valid JSON output.

  • a question with the Unique ID GroupID populates the Salesforce subjectId parameter in the Request Body. This means that the subjectId might change for every submitted form.

  • a question with the Unique ID RecordID populates the Salesforce entityId parameter

  • a question with the Unique ID TechnicianId, and a question with the Unique ID SupervisorId populate a Salesforce User ID that displays as a Mention in the Chatter message, and

  • the {{#pf:if}} element indicates a condition based on the answer to a question with the Unique ID FollowUpWorkOrder.

{{pf:outputFormat "JSON"}}
{
  "feedElementType": "FeedItem",
  "subjectId": "{{answers.GroupID.[0]}}",
  "body": {
    "messageSegments": [
      {
        "type": "Text",
        "text": "Work Order "
      },
      {
        "type": "EntityLink",
        "entityId": "{{answers.RecordID.[0]}}"
      },
      {
        "type": "Text",
        "text": "has been completed by "
      },
      {
        "type": "Mention",
        "id": "{{answers.TechnicianId.[0]}}"
      },
      {{#pf:if answers.FollowUpWorkOrder. [0] "is" "Yes"}}
      {
        "type": "Mention",
        "id": "{{answers.SupervisorId.[0]}}"
      },
      {
        "type": "Text",
        "text": " - A follow-up work order will be generated."
      },
      {{/pf:if}}
    ]
  }
}

Link the Data Destination and Document to the form

Prerequisites

Steps

  1. From the Forms & Integrations menu, navigate to the form you want to set up.

  2. Select EDIT FORM.

    Result: The form opens in edit mode.

  3. Select DESTINATIONS, and then select ADD A DATA DESTINATION.

    Result: A list of your Data DestinationsClosed A Data Destination specifies where to send data from a submitted form. You can use Data Destinations to automate data sharing and storage, routing data to a specific service (such as email or cloud storage) in several different formats. opens.

  4. Select the Salesforce Apex REST Data Destination you configured to post a Chatter Feed Item.

    Result: The system adds the Data Destination to your form. A page to configure the Data Destination on the form opens.

  5. In the Documents section, select the document you configured to post a Chatter Feed Item from the dropdown.

Tip:Remember to Save and Deploy the form when you’re ready to make it available to your field users.

Related topics