Set variables mandatory when state changes to closed complete on task

Pratiksha KC
Tera Guru

We have a variable set named "tasks", and all variables within this set are currently marked as mandatory. This variable set is only displayed in the variable editor section of the sc_task form.

Now, the requirement is:

The variables in the "tasks" variable set should only be mandatory when the state field on the sc_task form is set to Closed Complete.

How can we implement this logic effectively,  state is a field on the sc_task table and not part of the variable set?

 

I tried with standard UI policy, but it is making whole variable set mandatory as we have variables visibility on conditions.

 

For example we have variable - How many tasks required? 

Options- 1. One Task, 2. Two tasks, 3. Three tasks.

If user select one task, assignment group one get visible 

if user select two tasks , assignment group one and assignment group two get visible

 

PratikshaKC_0-1753386926479.png

 

But Using this UI policy all variables are visible on form load.

PratikshaKC_1-1753387003193.png

Script- 

g_form.setMandatory('tasks',true);
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Pratiksha KC 

2 ways

1) normal onChange client script on state field of sc_task and check if state is closed complete then make those mandatory

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    if (newValue == '3') {
        g_form.setMandatory('variables.variableName1', true);
        g_form.setMandatory('variables.variableName2', true);
        g_form.setMandatory('variables.variableName3', true);
    }

}

2) You need to ensure this is handled via the OOTB Close Task UI action as well

AnkurBawiskar_0-1753425377108.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

5 REPLIES 5

@Pratiksha KC 

for your further requirement you cannot determine the old value or new value of variable in normal onchange client script on state field.

To access old value and new value you will have to use onChange catalog client script on that variable only.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader