Dynamically Permit / Deny Grid Task Based on Row Values
There may be times when a task is not applicable for a given record type and its use should be denied.
Eg. “Unlock Account” and “Lock Account” tasks on a Users grid. If the account is currently “Unlocked” then the “Unlock Account” task is really not applicable for use with the row but the “Lock Account task would be.
While the task menu items cannot themselves be dynamically hidden, the run dialog can be made to deny the use of the task. We can use the bot Parameter JavaScript to control this.
For this example, we will have a grid of users and two available tasks: “Unlock Account” and “Lock Account”. The Unlock task should only be possible if the user account is locked, and vice-versa.
Step 1. Include “decision” column(s) in the grid.
As our “deciding” value upon which the decision will be made as to whether one task or the other is available for use will be the “Is Locked” value. If “true” then Unlock is possible, if “false” then Lock is possible. We have a Users grid in which we also include the IsLocked column (it could be “hidden” with the Column Chooser but we will leave it visible for this example).
Step 2. Create the Task bot including “decision” parameter(s).
We have an “Unlock Account” bot which only requires a UserPrincipalName to operate, however, we also added an “IsLocked” parameter in order to have the “decision” value come into the bot so a decision can be made as to whether the bot can be run or not.
While the run dialog will prevent the user of the task, we also add in a check in the bot to check that running it is permitted. This is a very simple check, a more advanced check could look directly into the user account to verify its current lock status. This is beyond the scope of this article and is left to the reader to enhance as needed.
Step 3. Define the “decision” in JavaScript
When the Run dialog appears we can have JavaScript run on various events on the parameters. For this example we will leverage the OnLoad event of the isLocked parameter (it could be the OnLoad event of any paramter, we are choosing isLocked simply because it is first).
Add the “decision” and “UI control” JavaScript to the OnLoad event of a parameter.
const decisionFieldName = 'isLocked'; //replace with decision field name
const denyRunValue = 'true'; //replace with value that denies run
if ($('#'+decisionFieldName).val() != denyRunValue) { //hide all input fields and disable run button
$('#tblScriptParamsForRun tbody tr').remove();
$('#tblScriptParamsForRun tbody').append('<tr><td>Unlock can only be used on locked accounts.</td</tr>');
$('#btnRunScriptWithParametersFromGrid').prop('disabled',true);
} else { //just hide the "decision" input field
$('#'+decisionFieldName).closest('tr').hide();
$('#'+decisionFieldName).closest('tr').prev().hide();
$('#btnRunScriptWithParametersFromGrid').prop('disabled',false);
}
The script template provided will handle simple text value based decisions for permitting/denying running the task. Simply replace the two values for the decision field name and the value when the run should be denied.
The decision could be more complex or allow for multiple permitted or denied values, eg.
const decisionFieldName = 'mailboxType';
const permittedTypes = ',EquipmentMailbox,RoomMailbox,SchedulingMailbox,SharedMailbox,';
if (!permittedTypes.includes($('#'+decisionFieldName).val())) {
The alteration of the decision criteria is beyond the scope of this article and is left the reader to enhance as needed.
Step 4. Bind the task to the grid
Finally, define and bind the tasks (the Unlock Task in this example) to the grid (Users). Map the decision column (IsLocked) to the bot’s decision parameter (#isLocked) in addition to mapping all the necessary fields for the task’s operation (in this case only the User Principal Name field is needed). Very importantly, the Show bound fields in dialog MUST be check for the field-based JavaScript to execute.
Final result
If selecting a row where the task should not be permitted to run, the run dialog is blank with a message advising use not possible.
Otherwise, the run dialog appears and operates as normal.