Resources »

Guides »

Bots »

Load arbitrary records into a behavior variable

Introduction

Sometimes your bot behavior needs to act on records that aren’t available as placeholders in the current event.

In these situations, you can use behavior variables to load matching records of any kind from any event.

In this guide, we’ll create an organization behavior that loads a list of the organization’s tickets into a behavior variable.

Create a bot

Let’s create a new bot called Example Bot:

  1. Navigate to Search » Bots.

  2. Click the (+) icon above the worklist to create a new bot.

  3. Enter the following details:
    • Name: Example Bot
    • Owner: Me
  4. Click the Save Changes button.

Create a behavior

Now we’ll create a behavior on the new bot:

  1. Open the card for Example Bot. You can click the link in the yellow notification above the worklist.

  2. Click the Behaviors button.

  3. Click the (+) icon above the worklist to create a new behavior.

  4. Enter the following details:

    • Event: Custom behavior on organization
    • Visibility: Private
    • Name: Load organization's tickets
    • Status: Enabled

Now let’s add a behavior variable for storing the organization’s ticket records.

  1. In the Variables section, click the Add button.

  2. Select List » Tickets.

  3. In the new List:Tickets entry, set it Private and name it Tickets.

  4. Click the Save Changes button to save the new behavior.

Load ticket records as an action

Finally, we’ll load ticket records into the behavior based on the current organization:

  1. Open the card for the Load organization’s tickets behavior. You can click the link in the yellow notification above the worklist.

  2. At the bottom of the card, click on Custom behavior on organization and select Add Actions from the menu.

  3. Enter the following details:
    • Title: Load ticket records for this organization
    • Status: Live
  4. In the Actions section, click the Action button and select Set » (Variable) Tickets from the menu.

  5. Check the box for and filter using quick search and enter the following query:
     
    	
     org.id:{{org_id}}
    	
     
     
  6. Click the Save Changes button to save the new action.

List variables can currently hold a maximum of 100 records. You can use loops to process more pages.

Use the variable’s records

The two most common ways to use a variable full of records are:

  • Displaying record placeholders in a script
  • Running actions on the records

Let’s look at an example of each.

In a script

  1. At the bottom of the card, click on Custom behavior on organization and select Add Actions from the menu.

  2. Enter the following details:
    • Title: Comment a summary on the organization
    • Status: Live
  3. In the Actions section, click the Action button and select Create » Comment from the menu.

  4. In the new Create comment action, set Comment on to Organization.

  5. In Content, use the following script:

     
    	
     Tickets:
    
     {% for ticket in var_tickets %}
     * [{{ticket.status}}] {{ticket._label}} ({{ticket.created|date_pretty}})
     {% endfor %}
    	
     
     
  6. Click the Save Changes button to save the action.

As a target in actions

  1. At the bottom of the card, click on Custom behavior on organization and select Add Actions from the menu.

  2. Enter the following details:
    • Title: Comment on each ticket
    • Status: Live
  3. In the Actions section, click the Action button and select Create » Comment from the menu.

  4. In the new Create comment action, set Comment on to (variable) Tickets.

  5. In Content, use the following script:

     
    	
     This ticket was opened by {{org__label}}
    	
     
     
  6. Click the Save Changes button to save the action.

You can use the Behavior run or Behavior schedule actions to run a Custom behavior on ticket behavior on each ticket record in the list.

Test the behavior

Let’s run the behavior in the simulator:

  1. Click the Simulator button at the top of the behavior’s card.

  2. Click the magnifying glass button (🔍) at the top of the simulator and select an organization that you know has tickets associated with it.

  3. Click the Simulate button.

First, you should see a list of tickets being loaded in the Set (variable) Tickets section. The simulator also gives you instructions for iterating the ticket variable and using its placeholders in scripts.

You can click on the green Set (variable) Tickets heading to hide it.

Next, you should see a comment being created on the target organization with a summary of its tickets. We used placeholders for the status, mask, subject, and created date.

Finally, you’ll see a comment being added on each ticket associated with the organization.

Close the simulator popup.

Conclusion

You now have the incredibly useful ability to load arbitrary records from any behavior.