Skip to contentSkip to navigationSkip to topbar
On this page

Use UI Actions


The Flex UI is constantly emitting event data that describes how the user is interacting with the Flex UI. As you write Plugins, the Actions Framework allows you to harness these events and define your own logic to describe how you want the Flex UI, CRM Data, or any other data, to change. You can register events before or after an action fires, or even replace the behavior of an Action.

Learn more about UI actions in our API documentation(link takes you to an external page).


Register and Invoke an Action

register-and-invoke-an-action page anchor
Actions.registerAction(name: string, action: ActionFunction, payloadUpdate?: PayloadUpdateFunction)

Registers a named action and provides the code that should be run when invoking it. The payloadUpdate method is optional, and used for triggering a callback to modify the payload given to action invocation.

Actions.invokeAction(name: string)

Invokes a named action. Returns a Promise.

Actions.addListener(name: string, action: ActionFunction)

Implements a custom function to be triggered either Before or After a specific Action.


Add and Remove Event Listeners

add-and-remove-event-listeners page anchor

You can add and remove event listeners.

Events supported:

before[eventName] (for example "beforeAcceptTask")
Called when a named action is triggered, but before the action body is run. You can abort the action that is provided with event body.

after[eventName]
Called after the action has stopped executing.

Note, the afterLogout event is not supported. Once the Worker has logged out, they will be returned to the Flex login screen.


Actions.replaceAction(name: string, action: ReplacedActionFunction)

Replaces the default implementation of a named action and provides an alternative implementation. The replaced action will be called with the same parameters as the original implementation, so you can add additional logic, then invoke the original action in your replacement function.


Common use cases and examples

common-use-cases-and-examples page anchor

Add an Action after a Task is accepted

add-an-action-after-a-task-is-accepted page anchor

Raises a JavaScript alert after an Agent has clicked to accept any Task.

1
flex.Actions.addListener(
2
"afterAcceptTask",
3
(payload) => alert("Triggered after event AcceptTask")
4
);

Ask for confirmation before accepting a Task

ask-for-confirmation-before-accepting-a-task page anchor

Generates a prompt before Task Acceptance; prevent that Action from running with an abort command if the user doesn't confirm.

1
flex.Actions.addListener(
2
"beforeAcceptTask",
3
(payload, abortFunction) => {
4
alert("Triggered before event AcceptTask");
5
if (!window.confirm("Are you sure you want to accept the task?")) {
6
abortFunction();
7
}
8
});

Detect a running call on Flex UI 2.0

detect-a-running-call-on-flex-ui-20 page anchor

Listens for an incoming call within the Flex UI and logs a message to the web console.

1
Actions.addListener("beforeAcceptTask", (payload) => {
2
if (payload.task.taskChannelUniqueName === 'voice') {
3
console.log("Call detected");
4
}
5
});

Customize an existing Action

customize-an-existing-action page anchor

Replaces the original Action for AcceptTask. Injects custom logic to alert about the replacement, but executes the original Action.

1
flex.Actions.replaceAction("AcceptTask", (payload, original) => {
2
return new Promise((resolve, reject) => {
3
alert("I have replaced this Action");
4
resolve();
5
}).then(() => original(payload));
6
});
7

Register a custom Action

register-a-custom-action page anchor

Registers a custom Action called MyAction, which makes an HTTP request. We then add a listener to the action CompleteTask which then invokes this custom Action. For example this could be used to update your CRM system.

1
flex.Actions.registerAction("MyAction", (payload) => {
2
return
3
fetch("https://my.server.backend.com/test")
4
.then(response => {
5
alert("Triggered MyAction with response " + JSON.stringify(response));
6
})
7
.catch(error => {
8
console.log(error);
9
throw error;
10
});
11
});
12
13
14
flex.Actions.addListener("afterCompleteTask", (payload) => {
15
return flex.Actions.invokeAction("MyAction");
16
});

Send a message after a Task is completed

send-a-message-after-a-task-is-completed page anchor

Sends a post-conversation message once the task is in a wrap-up state. Could be used to send a survey, or notify a user that the agent closed the session.

1
flex.Actions.replaceAction("WrapupTask", (payload, original) => {
2
// Only alter chat tasks:
3
if(payload.task.taskChannelUniqueName !== "chat") {
4
original(payload);
5
} else {
6
return new Promise((resolve, reject) => {
7
// Send the message:
8
flex.Actions.invokeAction("SendMessage", {
9
body: 'Thanks for chatting. Your session is now closed.',
10
conversationSid: payload.task.attributes.conversationSid
11
}).then((response) => {
12
// Wait until the message is sent to wrap-up the task:
13
resolve(original(payload));
14
});
15
});
16
}
17
});

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.