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

Ankur Bawiskar
Tera Patron
Tera Patron

@Pratiksha KC 

you can access form field value using g_form.getValue('state') in your Catalog client script

Then create your logic

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

Pratiksha KC
Tera Guru

Hi @JenniferRah  , @Ankur Bawiskar 

Tried below onChange catalog client script but not working. 

 

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

    // Get the value of the 'state' variable on the catalog item
    var stateValue = g_form.getValue('state');

    if (stateValue === '3') {
        applyMandatoryRules(true, newValue);
    } else {
        applyMandatoryRules(false, newValue);
    }
}

function applyMandatoryRules(isMandatory, taskCount) {
    if (taskCount === '1') {
        g_form.setMandatory('first_assignment_group', isMandatory);
        g_form.setMandatory('task_1_summary', isMandatory);
        g_form.setMandatory('task_1_description', isMandatory);

        g_form.setMandatory('second_assignment_group', false);
        g_form.setMandatory('task_2_summary', false);
        g_form.setMandatory('task_2_description', false);
        g_form.setMandatory('third_assignment_group', false);
        g_form.setMandatory('task_3_summary', false);
        g_form.setMandatory('task_3_description', false);

    } else if (taskCount === '2') {
        g_form.setMandatory('first_assignment_group', isMandatory);
        g_form.setMandatory('task_1_summary', isMandatory);
        g_form.setMandatory('task_1_description', isMandatory);

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

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

    } else if (taskCount === '4') {
        g_form.setMandatory('first_assignment_group', isMandatory);
        g_form.setMandatory('task_1_summary', isMandatory);
        g_form.setMandatory('task_1_description', isMandatory);

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

        g_form.setMandatory('third_assignment_group', isMandatory);
        g_form.setMandatory('task_3_summary', isMandatory);
        g_form.setMandatory('task_3_description', isMandatory);

    } else {
        // Fallback: clear all
        g_form.setMandatory('first_assignment_group', false);
        g_form.setMandatory('task_1_summary', false);
        g_form.setMandatory('task_1_description', false);

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

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

You need to make it an onSubmit client script and make sure the "Applies on Catalog Tasks" is checked.

 

JenniferRah_0-1753808032769.png

If it still isn't working, then I would need to know if you're running it from a client or Portal or both. Also, what specifically isn't working?

Hi @JenniferRah 

Created onSubmit client script. But form is getting saved even if fields are empty when state is closed complete.

 

PratikshaKC_0-1753810336967.png

Script-

function onSubmit() {
  var stateValue = g_form.getValue('state');
  var taskCount = g_form.getValue('number_of_required_tasks');

  if (stateValue !== '3') {
    // Not Closed Complete → make all fields non-mandatory
    g_form.setMandatory('first_assignment_group', false);
    g_form.setMandatory('task_1_summary', false);
    g_form.setMandatory('task_1_description', false);

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

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

    return true;
  }

  // If state is Closed Complete, apply based on number of tasks
  if (taskCount === '1') {
    // Task 1 mandatory
    g_form.setMandatory('first_assignment_group', true);
    g_form.setMandatory('task_1_summary', true);
    g_form.setMandatory('task_1_description', true);

    // Task 2 & 3 non-mandatory
    g_form.setMandatory('second_assignment_group', false);
    g_form.setMandatory('task_2_summary', false);
    g_form.setMandatory('task_2_description', false);
    g_form.setMandatory('third_assignment_group', false);
    g_form.setMandatory('task_3_summary', false);
    g_form.setMandatory('task_3_description', false);

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

    g_form.setMandatory('second_assignment_group', true);
    g_form.setMandatory('task_2_summary', true);
    g_form.setMandatory('task_2_description', true);

    // Task 3 non-mandatory
    g_form.setMandatory('third_assignment_group', false);
    g_form.setMandatory('task_3_summary', false);
    g_form.setMandatory('task_3_description', false);

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

    g_form.setMandatory('second_assignment_group', true);
    g_form.setMandatory('task_2_summary', true);
    g_form.setMandatory('task_2_description', true);

    g_form.setMandatory('third_assignment_group', true);
    g_form.setMandatory('task_3_summary', true);
    g_form.setMandatory('task_3_description', true);

  } else {
    // Fallback — mark all non-mandatory
    g_form.setMandatory('first_assignment_group', false);
    g_form.setMandatory('task_1_summary', false);
    g_form.setMandatory('task_1_description', false);

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

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

  return true; // allow form submission
}

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.