Making a variable mandatory only on state choice changes

Sonal2
Tera Contributor

Hi All
After submitting the catalog item I want to make a variable set mandatory in sc_req_item table workspace only when state choice is changes to closed complete. I have tried on change client script as well but the field name cant be state in catalog item. Can someone help on this.

2 ACCEPTED SOLUTIONS

In this example, 'state' is undefined.  Since this is running onChange of the State field, you can use newValue to retrieve the value of the state field.  Also, you'll want to include an else block so that the variable remains not mandatory if the user switched to the Closed Complete State then another State before saving the record.  The variable.variableName syntax is optional and can be simplified to just use the variable name:

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

    var variableValue = g_form.getValue('variableName');
    if (variableValue == '' && newValue == 3) {
        g_form.setMandatory('variableName', true);
    } else {
		g_form.setMandatory('variableName', false);
    }
	

 

View solution in original post

@Sonal2 

small mistake in my script.

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

    var variableValue = g_form.getValue('variables.variableName');
    if (variableValue == '' && g_form.getValue('state') == 3) {
        g_form.setMandatory('variables.variableName', true);
    }
}

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

6 REPLIES 6

In this example, 'state' is undefined.  Since this is running onChange of the State field, you can use newValue to retrieve the value of the state field.  Also, you'll want to include an else block so that the variable remains not mandatory if the user switched to the Closed Complete State then another State before saving the record.  The variable.variableName syntax is optional and can be simplified to just use the variable name:

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

    var variableValue = g_form.getValue('variableName');
    if (variableValue == '' && newValue == 3) {
        g_form.setMandatory('variableName', true);
    } else {
		g_form.setMandatory('variableName', false);
    }
	

 

@Sonal2 

small mistake in my script.

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

    var variableValue = g_form.getValue('variables.variableName');
    if (variableValue == '' && g_form.getValue('state') == 3) {
        g_form.setMandatory('variables.variableName', true);
    }
}

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