MS Teams – Chats – Create Chat Message
MS Teams – Chats – Create Chat Message
Create a message in a chat.
Action details
| Property | Value |
|---|---|
| Action label | MS Teams – Chats – Create Chat Message |
| Category | CloudTools |
| Apex class and method | CreateMSTeamsChatMessage.createChatMessage |
| Input type | CreateMSTeamsChatMessage.CreateMSTeamsChatMessageInput |
| Output type | List<MSTeamsChatMessage> |
| Authorization | Delegated access for normal posting; application permission is migration-only. |
| Microsoft Graph request | POST /me/chats/{chatId}/messages (delegated); /chats/{chatId}/messages (application path in implementation) |
Prerequisites and permissions
Configure a CloudTools Connector with Connector Type set to Microsoft 365. Complete Microsoft sign-in for OAuth Delegated (Authorization Code). For endpoints supporting OAuth Application (Client Credentials), grant application permissions and administrator consent. The identity must have access to the selected resource. See Creating an Entra ID App and Integration Connectors for setup.
The action permission provider declares these alternatives; they are not cumulative requirements and not all are least privileged. Resource-specific permissions require consent for the relevant Teams resource.
| Permission type | Declared alternatives |
|---|---|
| Delegated | ChatMessage.Send, Chat.ReadWrite, Group.ReadWrite.All |
| Application | Teamwork.Migrate.All |
Endpoints: /chats/{chat-id}/messages. Application permissions support migration only, not normal message posting. Group.ReadWrite.All is listed for backward compatibility.
See the Microsoft operation and permissions reference.
Inputs
| Input | Apex field | Type | Required | Description |
|---|---|---|---|---|
| User Id | userId | String | No | Optional user identifier for delegated authorization code flow |
| Chat Id | chatId | String | Yes | MS Teams chat identifier |
| Content | content | String | Yes | Message body content |
| Content Type | contentType | String | No | Body content type: text or html (defaults to html) |
| Subject | subject | String | No | Optional message subject |
| Importance | importance | String | No | Optional message importance (for example: normal, high, urgent) |
Supply a nonempty list with a non-null first request. This implementation processes only the first request and does not bulk-process Flow interviews. Global inputs do not expose connectorId; Flow and subscriber Apex use CloudTools connector selection. Use Microsoft Graph IDs, not Salesforce IDs or browser links. Resolve dynamic values before invoking this action.
Outputs
Returns one MSTeamsChatMessage inside a list, or an empty list if no resource is mapped. Check for an empty result before reading fields.
| Output | Apex field | Type | Description |
|---|---|---|---|
| Id | id | String | Unique identifier of the message |
| Chat Id | chatId | String | Identifier of the chat that contains the message |
| Created Date Time | createdDateTime | String | Created timestamp for the message |
| Deleted Date Time | deletedDateTime | String | Deleted timestamp for the message |
| ETag | eTag | String | Version number of the message |
| Importance | importance | String | Importance of the message |
| Last Modified Date Time | lastModifiedDateTime | String | Last modified timestamp for the message |
| Last Edited Date Time | lastEditedDateTime | String | Last edited timestamp for the message |
| Locale | locale | String | Locale for the message |
| Message Type | messageType | String | Type of the message |
| Reply To Id | replyToId | String | Identifier of the parent/root message |
| Subject | subject | String | Subject of the message |
| Summary | summary | String | Summary text for the message |
| Web URL | webUrl | String | Link to the message in Teams |
| Body | body | MSTeamsMessageBody | Content body of the message |
| From | fromIdentity | MSTeamsMessageFromIdentitySet | Sender identity set for the message |
| Channel Identity | channelIdentity | MSTeamsMessageChannelIdentity | Channel identity for channel messages |
| Attachments | attachments | List<MSTeamsMessageAttachment> | Attachments associated with the message |
| Mentions | mentions | List<MSTeamsMessageMention> | Mentions included in the message |
| Message History | messageHistory | List<MSTeamsMessageHistoryItem> | History items for message activities |
| Reactions | reactions | List<MSTeamsMessageReaction> | Reactions for the message |
| Hosted Contents | hostedContents | List<MSTeamsMessageHostedContent> | Hosted contents for the message |
| Policy Violation | policyViolation | MSTeamsMessagePolicyViolation | Policy violation details for the message |
| Event Detail | eventDetail | MSTeamsMessageEventDetail | Event detail for event messages |
| Replies | replies | List<MSTeamsChatMessage> | Replies for this message |
| OData Type | odataType | String | OData type for the message |
| OData ETag | odataEtag | String | OData ETag for the message |
| Additional Properties | additionalProperties | List<DynamicObject> | Additional properties from user-defined query |
Behavior and limits
MSTeamsAction uses /me/chats for delegated access, ignoring User Id in that path. Its application user-chat path is /users/{userId}/chats; message mutations instead use /chats/{chatId} where shown above. Check the Microsoft reference if a user-scoped path is rejected.
Normal message posting requires delegated authorization. Teamwork.Migrate.All does not enable routine application message posting. Content Type defaults to html and is normalized to text or html. The input supports only the listed message properties, not arbitrary attachments, mentions, or hosted content.
Use in Flow
Add MS Teams – Chats – Create Chat Message where supported. CloudTools Flow automation does not support delegated connections. Normal message posting therefore requires a delegated custom integration; migration permission is not a routine Flow posting option.
Use in custom Apex
This subscriber-org example uses the Apptigent namespace. Replace example identifiers and values before use. Configure the connector, arrange authorization first, run in a context permitting callouts, and handle exceptions and empty results.
Apptigent.CreateMSTeamsChatMessage.CreateMSTeamsChatMessageInput request =
new Apptigent.CreateMSTeamsChatMessage.CreateMSTeamsChatMessageInput();
request.userId = 'd7b45f12-8a16-4a1f-95f1-dfde7f2af91b';
request.chatId = '19:1c5b01696d2e4a179c292bc9cf04e63b@thread.v2';
request.content = '<div>Hello chat</div>';
request.contentType = 'html';
request.subject = 'Project Update';
request.importance = 'normal';
List<Apptigent.MSTeamsChatMessage> results =
Apptigent.CreateMSTeamsChatMessage.createChatMessage(
new List<Apptigent.CreateMSTeamsChatMessage.CreateMSTeamsChatMessageInput>{ request }
);
System.debug(results);
Use in a Lightning Web Component
Use createMSTeamsChatMessage from msTeamsConnectorLib through MSTeamsConnectorController. Set request.connectorId to select a configured connector. These c/ imports apply in the same namespace. The controller uses a shared request DTO; its field names differ from the global invocable input.
| Global input | LWC request mapping |
|---|---|
userId | request.userId |
chatId | request.chatId |
content | request.bodyContent |
contentType | request.bodyContentType |
subject | request.subject |
importance | request.importance |
teamsActionExample.js
import { LightningElement } from 'lwc';
import { createMSTeamsChatMessage } from 'c/msTeamsConnectorLib';
export default class TeamsActionExample extends LightningElement {
result = null;
errorMessage = '';
isRunning = false;
completed = false;
async handleRun() {
if (this.isRunning || this.completed) return;
this.isRunning = true;
this.errorMessage = '';
try {
const request = { connectorId: 'REPLACE_WITH_CONNECTOR_RECORD_ID' };
request.userId = 'd7b45f12-8a16-4a1f-95f1-dfde7f2af91b';
request.chatId = '19:1c5b01696d2e4a179c292bc9cf04e63b@thread.v2';
request.bodyContent = '<div>Hello chat</div>';
request.bodyContentType = 'html';
request.subject = 'Project Update';
request.importance = 'normal';
this.result = await createMSTeamsChatMessage(request);
this.completed = true;
} catch (error) {
this.errorMessage = error.requiresInteractiveAuthorization
? 'Sign in to Microsoft for the connector, then try again.'
: error.message || 'The Teams action failed.';
} finally {
this.isRunning = false;
}
}
}
teamsActionExample.html
<template>
<lightning-button label="Run action" onclick={handleRun}
disabled={isRunning}></lightning-button>
<p role="alert">{errorMessage}</p>
</template>
The library resolves to one mapped object or null. rawJson serializes the mapped Apex model rather than the untouched Microsoft response. Message body, membership roles, and nested properties can be flattened or serialized in JavaScript DTOs.
Use getMSTeamsAuthorizationState, beginMSTeamsAuthorization, and createMSTeamsOAuthMessageHandler to arrange delegated sign-in. The library normalizes errors but does not automatically sign in or retry. Reset completed for a new deliberate operation. Inspect resource state before retrying a mutation with an uncertain outcome.
Error handling and troubleshooting
| Symptom | What to check |
|---|---|
| Required fields error | Supply a nonempty request list, a non-null first request, and required or conditionally required values. For LWC, check controller mappings. |
| Authorization or access denied | Check selected connector, delegated versus application support, consent, Teams app installation where required, and access to the resource. |
| Resource not found | Check team, channel, chat, message, membership, and app installation IDs; these are distinct identifiers. |
| Missing data | Check pagination, query support, empty-response behavior, and documented model mapping. |
| Operation rejected | Inspect the Microsoft error, endpoint restrictions, supported properties, message authorization limits, and notification configuration. |