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

Shruti
Mega Sage
Mega Sage

Hi,

 

Try to use  g_form.variables.variable_name

function onCondition() {


    if (g_form.getValue('variables.no_of_tasks') == '1') {
        g_form.setMandatory('variables.task_1', true);
        g_form.setMandatory('variables.group_1', true);
    } else if (g_form.getValue('variables.no_of_tasks') == '2') {
        g_form.setMandatory('variables.task_1', true);
        g_form.setMandatory('variables.group_1', true);
        g_form.setMandatory('variables.task_2', true);
        g_form.setMandatory('variables.group_2', true);
    }


}

  

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

Hi @Ankur Bawiskar 

I tried the script and it's working fine.
Issue is - when I'm trying to change - "How many task required" variable field-

Old value - one task and new value - two tasks, 
It's not making mandatory for two tasks as shown in SS

PratikshaKC_0-1753781651656.png

 

Script-

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

    // Only apply mandatory logic if state is Closed Complete
    if (g_form.getValue('state') === '3') {
        applyMandatoryRules(true);
    } else {
        applyMandatoryRules(false);
    }
}

function applyMandatoryRules(isMandatory) {
    var taskCount = g_form.getValue('variables.number_of_required_tasks');

    // Task 1
    g_form.setMandatory('variables.first_assignment_group', isMandatory);
    g_form.setMandatory('variables.task_1_summary', isMandatory);
    g_form.setMandatory('variables.task_1_description', isMandatory);

    // Task 2
    if (taskCount === '2' || taskCount === '4') {
        g_form.setMandatory('variables.second_assignment_group', isMandatory);
        g_form.setMandatory('variables.task_2_summary', isMandatory);
        g_form.setMandatory('variables.task_2_description', isMandatory);
    } else {
        g_form.setMandatory('variables.second_assignment_group', false);
        g_form.setMandatory('variables.task_2_summary', false);
        g_form.setMandatory('variables.task_2_description', false);
    }

    // Task 3
    if (taskCount === '4') {
        g_form.setMandatory('variables.third_assignment_group', isMandatory);
        g_form.setMandatory('variables.task_3_summary', isMandatory);
        g_form.setMandatory('variables.task_3_description', isMandatory);
    } else {
        g_form.setMandatory('variables.third_assignment_group', false);
        g_form.setMandatory('variables.task_3_summary', false);
        g_form.setMandatory('variables.task_3_description', false);
    }
}

 

@Pratiksha KC 

this is other issue and doesn't relate to your main requirement where you wanted to make variables mandatory on task closure

I believe I already shared answer to your main question.

AnkurBawiskar_0-1753782687616.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