Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Making a variable mandatory only on state choice changes

Not applicable

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

@Community Alums 

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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Brad Bowman
Mega Patron

You can do this with a Client Script (not Catalog) that applies to the sc_req_item table, not the Catalog Item.  Use g_form methods with the name of the RITM field or Catalog Item variable.

Ankur Bawiskar
Tera Patron

@Community Alums 

Agree with Brad.

It should be onChange client script on State field and simply check if variable is populated

If not then make it mandatory

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

    var variableValue = g_form.getValue('variables.variableName');
    if (variableValue == '' && 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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Not applicable

Hi @Ankur Bawiskar its not working.

Not applicable

Hi @Ankur Bawiskar , This script is not working

Sonal2_0-1739199591886.png