Mandatory Variables on Close
I have seen many requests to have variables that were hidden on the initial submission form visible on the Catalog Task record. These variables would only be mandatory upon Close, as well as only mandatory for those tasks that require it.
What needs to be done is to have the variable not visible on the form, but is visible on the Catalog Task. That variable only becomes mandatory for one particular task even though it may be visible on more than one task.
In order to make this solution more universal, this article will cover creating a sample Catalog Item with sample variables, a sample Catalog UI Policy, a sample workflow, a sample Business Rule, a sample UI Policy, and a sample UI Action
Catalog Item
Sample Request with Task Variables
Name: Sample Request with Task Variables
Workflow: REQ - Sample WF
Short description: Sample Request with Task Variables
Variables
s_form_var_01
Name: s_form_var_01
Type: Single Line Text
Order: 100
Question: Form Variable One
s_form_var_02
Name: s_form_var_02
Type: Single Line Text
Order: 110
Question: Form Variable Two
s_task_var_01
Name: s_task_var_01
Type: Single Line Text
Order: 800
Question: Task Variable One
s_task_var_02
Name: s_task_var_02
Type: Single Line Text
Order: 810
Question: Task Variable Two
s_task_var_03
Name: s_task_var_03
Type: Single Line Text
Order: 820
Question: Task Variable Three
Catalog UI Policies
In order to hide the task variables on the form as well as the Requested Item record, a Catalog UI Policy needs to be created:
Hide Task Variables
Order: 200
Applies on a Catalog Item view: true
Applies on Requested Items: true
Applies on Catalog Tasks: false
Short Description: Hide Task Variables
Catalog UI Policy Actions
s_task_var_01
Name: s_task_var_01
Mandatory: Leave alone
Visible: False
Read Only: Leave alone
s_task_var_02
Name: s_task_var_02
Mandatory: Leave alone
Visible: False
Read Only: Leave alone
s_task_var_03
Name: s_task_var_03
Mandatory: Leave alone
Visible: False
Read Only: Leave alone
Workflow
REQ - Sample WF
Name: REQ - Sample WF
Table: Requested Item [sc_req_item]
Workflow Actions
Within your workflow, create three Catalog Tasks:
Task One
Name: Task One
Wait for completion: true
Fulfillment group: Service Desk
Short Description: This is Task One
Variables on Task Form:
s_task_var_01
s_task_var_02
Task Two
Name: Task Two
Wait for completion: true
Fulfillment group: Service Desk
Short Description: This is Task Two
Variables on Task Form:
s_task_var_02
s_task_var_03
Task Three
Name: Task Three
Wait for completion: true
Fulfillment group: Service Desk
Short Description: This is Task Three
Variables on Task Form:
s_task_var_01
s_task_var_02
s_task_var_03
Business Rules
What this business rule does is find the name of the workflow activity and store it in the g_scratchpad object. This can then be used to determine which Catalog task the UI Policy is running against.
Set g_scratchpad for sc_task
Name: Set g_scratchpad for sc_task
Table: Catalog Task [sc_task]
Order: 100
When: display
Condition:
Script:
(function executeRule(current, previous /*null when async*/) {
g_scratchpad.wf_activity = current.wf_activity.getDisplayValue();
})(current, previous);
UI Policies
This is a UI Policy, not a Catalog UI Policy. The reason for this is because the conditions are triggered from the Catalog Task.
Make Variables Mandatory on Close
Table: Catalog Task [sc_task]
Reverse if false: true
Order: 300
Global: true
Run scripts: true
Short description: Make Variables Mandatory on Close
Conditions:
State | is | Closed Complete
Execute if True: true
function onCondition() {
if (g_scratchpad.wf_activity == 'Task One') {
g_form.setMandatory('variables.s_task_var_01', true);
}
if (g_scratchpad.wf_activity == 'Task Two') {
g_form.setMandatory('variables.s_task_var_02', true);
}
if (g_scratchpad.wf_activity == 'Task Three') {
g_form.setMandatory('variables.s_task_var_03', true);
}
}
Execute if False:
function onCondition() {
g_form.setMandatory('variables.s_task_var_01', false);
g_form.setMandatory('variables.s_task_var_02', false);
g_form.setMandatory('variables.s_task_var_03', false);
}
UI Actions
Because the user may use the Close Task button instead of changing the state, you need to put the check into the UI Action
Close Task
Name: Close Task
Table: Catalog Task [sc_task]
Order: 100
Action name: close_task
Client: true
Onclick: closeTaskCheck();
Script:
function closeTaskCheck() {
if (g_scratchpad.wf_activity == 'Task One') {
g_form.setMandatory('variables.s_task_var_01', true);
}
if (g_scratchpad.wf_activity == 'Task Two') {
g_form.setMandatory('variables.s_task_var_02', true);
}
if (g_scratchpad.wf_activity == 'Task Three') {
g_form.setMandatory('variables.s_task_var_03', true);
}
gsftSubmit(null, g_form.getFormElement(), 'close_task');
}
if (typeof window == 'undefined') serverCloseTask();
function serverCloseTask() {
current.state = 3;
current.update();
}
https://www.servicenow.com/community/developer-articles/mandatory-variables-on-close/ta-p/2323278