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?

 

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

 

I tried with standard onChange Client script on state field,

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');

    if (taskCount === '1') {
        // Task 1 only
        g_form.setMandatory('variables.first_assignment_group', isMandatory);
        g_form.setMandatory('variables.task_1_summary', isMandatory);
        g_form.setMandatory('variables.task_1_description', isMandatory);

        // Clear others
        g_form.setMandatory('variables.second_assignment_group', false);
        g_form.setMandatory('variables.task_2_summary', false);
        g_form.setMandatory('variables.task_2_description', false);
        g_form.setMandatory('variables.third_assignment_group', false);
        g_form.setMandatory('variables.task_3_summary', false);
        g_form.setMandatory('variables.task_3_description', false);

    } else if (taskCount === '2') {
        // Task 1 & 2
        g_form.setMandatory('variables.first_assignment_group', isMandatory);
        g_form.setMandatory('variables.task_1_summary', isMandatory);
        g_form.setMandatory('variables.task_1_description', isMandatory);

        g_form.setMandatory('variables.second_assignment_group', isMandatory);
        g_form.setMandatory('variables.task_2_summary', isMandatory);
        g_form.setMandatory('variables.task_2_description', isMandatory);

        // Clear task 3
        g_form.setMandatory('variables.third_assignment_group', false);
        g_form.setMandatory('variables.task_3_summary', false);
        g_form.setMandatory('variables.task_3_description', false);

    } else if (taskCount === '4') {
        // Task 1, 2 & 3
        g_form.setMandatory('variables.first_assignment_group', isMandatory);
        g_form.setMandatory('variables.task_1_summary', isMandatory);
        g_form.setMandatory('variables.task_1_description', isMandatory);

        g_form.setMandatory('variables.second_assignment_group', isMandatory);
        g_form.setMandatory('variables.task_2_summary', isMandatory);
        g_form.setMandatory('variables.task_2_description', isMandatory);

        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 {
        // No valid taskCount selected - clear all
        g_form.setMandatory('variables.first_assignment_group', false);
        g_form.setMandatory('variables.task_1_summary', false);
        g_form.setMandatory('variables.task_1_description', false);

        g_form.setMandatory('variables.second_assignment_group', false);
        g_form.setMandatory('variables.task_2_summary', false);
        g_form.setMandatory('variables.task_2_description', false);

        g_form.setMandatory('variables.third_assignment_group', false);
        g_form.setMandatory('variables.task_3_summary', false);
        g_form.setMandatory('variables.task_3_description', false);
    }
}
 
But, 

I selected state as closed complete and not saved yet the form and now 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-1753790580139.png

 

IK that we are creating onChange script on state field but how to achieve our goal? 

1 ACCEPTED SOLUTION

You have to check the values of the fields in the onSubmit and then return false if you don't want to save it. It would look something like this:

function onSubmit() {
    var retval = true;
    if (g_form.getValue('state') >= 3) { //Closing task
                if (g_form.getValue(fieldToCheck) == '' ) {//add all your required fields here
                    retval = false;
                }
            return retval;
        }
}

 

If you want to actually mark them all as required, you can do that in your onChange script or you can do it in the onSubmit. However, the thing that will stop it from being submitted is you checking the values and returning false if they are blank.

View solution in original post

9 REPLIES 9

JenniferRah
Mega Sage

Did you try your client script as an onSubmit catalog client script off of the Catalog Item itself? You should be able to do that and just select for it to run on Catalog Tasks.

Ankur Bawiskar
Tera Patron
Tera Patron

@Pratiksha KC 

Would you mind closing your earlier questions by marking appropriate response as correct?

Members have invested their time and efforts in helping you.

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

@Ankur Bawiskar 

Please check.       

@Pratiksha KC 

Please check my below response.

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