Use Command Click Scripts to move users between tags

This article describes what is Command Click Scripts and how to use it.

Introduction

Command Click Scripts allows you to assign or remove a user from a tag after a command is executed successfully.

Requirements

Creating the command click script

1. To create a Command Click Script, go to menu Development > App Builder.

mceclip0.png

2. Click on + New App

mceclip1.png

3. Set a name to your new app, then click on Save.

mceclip0.png

4. Now create a + New Click Script inside your new app.

mceclip3.png

5. Set a name to your new Click Script, then select the Command kind.

Set the new click script to Enabled.

mceclip4.png

6. Click on Edit button and paste the following code into the editor:

var tagAdding, tagRemoving;
 
if (command.name == 'ModeA') {
  tagAdding = tags.getTagByName('First');
  tagRemoving = tags.getTagByName('Second');
}
else if (command.name == 'ModeB') {
  tagAdding = tags.getTagByName('Second');
  tagRemoving = tags.getTagByName('First');
}
 
if (tagAdding != null) {
  tags.updateUsers(tagAdding.id, [user.id], []);
}
 
if (tagRemoving != null) {
  tags.updateUsers(tagRemoving.id, [], [user.id]);
}

Note: the command should run successfully before saving the App. Make sure you're not missing any of the requisites at the beginning of this guide.

7. In Command Tags section of the click script, select the Command Tags associated with this script. The script will be executed after successful completion of a command only if the command is in this Command Tag.

mceclip5.png

Finally, click on Save.

Results

Running the script from the editor once with one of the two commands specified in the Exposed objects and test values section under command.name should immediately move a user to the right tag and validate the script.

Now save the script and run the other command. After the command is completed you will see that the user has moved to the other tag.