See related
No related articles

Dynamically assign an agent's team to conversations

Last Update: Oct 2024 • Est. Read Time: 8 MIN
To check plan availability, see the pricing page.

While business rules let you assign conversations to a specific team, you may instead prefer a conversation's assigned team to be updated dynamically based on an agent's work session and the team they were logged in to when the conversation was assigned to them. Or, you may want to remove the assigned team automatically when a user is unassigned, so that the conversation's assigned team is refreshed when a different user is assigned. Workflows can automate both of these processes.

Who can access this feature?
User typesAdmins can access workflows and API keys.


In this article

Prerequisites

Before following this guide, you should already be comfortable with the basics of using workflows. Workflows overview

We also recommend being familiar with creating API keys and adding workflow variables.

Create an API key

First, you'll need to set up the API key to use with the workflow.

  1. Create an API key.
  2. When setting up your new API key, assign it with the roles org.user.routing.read and org.permission.routing.read.
  3. Then, keep the token in a secure location to use later in this guide.

Create the workflow

Next, you'll set up the workflow to assign conversations.

  1. Go to Settings> Platform > Workflows.
  2. Select Add Workflow > Custom Workflow.
  3. The New Workflow modal will appear. Fill out the Workflow Title. You can leave the Description field blank because the code snippet below will provide this for you.
  4. Leave the callable workflow option turned off.
  5. Select Create to save the workflow. You'll be taken to the Workflow Editor where the workflow can be customized.

Add the workflow JSON

Then, Use the Workflow Editor to paste in the workflow JSON code provided below. This JSON will take care of most of the workflow creation.

  1. In the Workflow Editor, open the Workflow Optionsmenu and select Edit Workflow JSON.
  2. A modal appears where you can edit the code for this workflow. Select and delete all the current JSON code.
  3. Copy the below code block, and paste it into the JSON text field in your Workflow Editor. Then, select Update to save the changes to the workflow and dismiss the JSON editor.
  {
    "description": "Assigns the team to a conversation based on the assigned user's team in their current work session. Also unassigns the team if a user is unassigned.",
    "name": "assign-team-on-user-assignment",
    "steps": [
      {
        "transitions": [
          {
            "target": "MtNv8EMuK",
            "condition": {
              "op": "and",
              "values": [
                {
                  "op": "exists",
                  "values": [
                    "/#steps.K_bX_LCYn.id",
                    ""
                  ]
                },
                {
                  "op": "dne",
                  "values": [
                    "/#steps.K_bX_LCYn.deletedAt",
                    ""
                  ]
                }
              ]
            },
            "meta": {
              "name": "Assigned User Found"
            }
          }
        ],
        "errorCases": [],
        "id": "K_bX_LCYn",
        "action": "kustomer.user.find-by-id",
        "params": {
          "id": "/#steps.1.assignedUsers.0"
        },
        "meta": {
          "displayName": "Find Assigned User"
        },
        "appVersion": "kustomer-^1.9.2"
      },
      {
        "transitions": [
          {
            "target": "hLKnq3Vye",
            "condition": {
              "op": "eq",
              "values": [
                "/#steps.MtNv8EMuK.response.statusCode",
                "200"
              ]
            },
            "meta": {
              "name": "User is in Online Work Session"
            }
          }
        ],
        "errorCases": [],
        "id": "MtNv8EMuK",
        "action": "kustomer.rest-api.json",
        "params": {
          "uri": "https://api.kustomerapp.com/v1/routing/users/{{{steps.K_bX_LCYn.id}}}/work-sessions/current",
          "method": "GET",
          "headers": {
            "Authorization": "Bearer",
            "Content-Type": "application/json"
          },
          "json": true
        },
        "appVersion": "kustomer-^1.9.2",
        "meta": {
          "displayName": "Find Assigned User's Current Work Session"
        }
      },
      {
        "transitions": [
          {
            "target": "kAEdWGR5T",
            "condition": {
              "op": "exists",
              "values": [
                "/#steps.MtNv8EMuK.response.body.data.relationships.team.data.id",
                ""
              ]
            },
            "meta": {
              "name": "Assigned User Is Logged in to a Team"
            }
          }
        ],
        "errorCases": [],
        "id": "hLKnq3Vye"
      },
      {
        "transitions": [],
        "errorCases": [],
        "id": "kAEdWGR5T",
        "action": "kustomer.conversation.assign-team",
        "params": {
          "id": "/#steps.1.id",
          "team": "/#steps.MtNv8EMuK.response.body.data.relationships.team.data.id"
        },
        "appVersion": "kustomer-^1.9.2"
      },
      {
        "transitions": [
          {
            "target": "U2B0NRL5q",
            "condition": {
              "op": "exists",
              "values": [
                "/#steps.1.assignedTeams.0",
                ""
              ]
            },
            "meta": {
              "name": "Conversation Has Assigned Team"
            }
          }
        ],
        "errorCases": [],
        "id": "FrGZLHeS5"
      },
      {
        "transitions": [],
        "errorCases": [],
        "id": "U2B0NRL5q",
        "action": "kustomer.conversation.assign-team",
        "params": {
          "id": "/#steps.1.id",
          "team": []
        },
        "meta": {
          "displayName": "Unassign Team from Conversation"
        },
        "appVersion": "kustomer-^1.9.2"
      },
      {
        "transitions": [
          {
            "target": "K_bX_LCYn",
            "condition": {
              "op": "or",
              "values": [
                {
                  "op": "dne",
                  "values": [
                    "/#steps.1.changes.attributes.assignedUsers.before.0",
                    ""
                  ]
                },
                {
                  "op": "neq",
                  "values": [
                    "/#steps.1.changes.attributes.assignedUsers.before.0",
                    "/#steps.1.changes.attributes.assignedUsers.after.0"
                  ]
                }
              ]
            },
            "meta": {
              "name": "Conversation Previously Unassigned or Assigned to a Different User"
            }
          }
        ],
        "errorCases": [],
        "id": "7jVAP0q_Y"
      }
    ],
    "trigger": {
      "transitions": [
        {
          "target": "7jVAP0q_Y",
          "condition": {
            "op": "exists",
            "values": [
              "/#steps.1.changes.attributes.assignedUsers.after.0",
              ""
            ]
          },
          "meta": {
            "name": "Conversation is Assigned to a User"
          }
        },
        {
          "target": "FrGZLHeS5",
          "condition": {
            "op": "and",
            "values": [
              {
                "op": "exists",
                "values": [
                  "/#steps.1.changes.attributes.assignedUsers.before.0",
                  ""
                ]
              },
              {
                "op": "dne",
                "values": [
                  "/#steps.1.changes.attributes.assignedUsers.after.0",
                  ""
                ]
              }
            ]
          },
          "meta": {
            "name": "Conversation is Unassigned from a User"
          }
        }
      ],
      "eventName": "kustomer.conversation.update",
      "id": "1",
      "appVersion": "kustomer-^1.9.2"
    }
  }

The workflow will look like this:

Add the API key to the workflow

  1. Reopen the  Workflow Optionsmenu and select Workflow Variables.
  2. Add the saved token from the API key you created earlier as a workflow variable. Name the workflow variable as desired and add the token in the Value field.
  3. Save the variable, select Close to exit the workflow editor, and save the updated workflow.

Customize the workflow

  1. In Step 5 of the workflow, select the key icon in the REST API JSON action step to access the workflow variable saved, and copy the workflow variable.
  2. Add a single space after Bearer in the Headers section, and paste the copied value which is the API token needed for authorization. Set the JSON (boolean) field to true.
  3. If there are any other changes to the workflow that you'd like to make, do so now. When you're finished, Save all and other workflow changes, and use the toggle to activate the workflow.