The CreatorCon Call for Content is officially open! Get started here.

Client Script onChange does not work first time around

Lee Jones
Tera Contributor

I'm relatively new to ServiceNow Client Scripts. I'm trying to write a script that makes the 'comments' field mandatory specifically when incident 'state' changes to newValue In Progress (2), from oldValue New (1).

 

The script seems to work successfully, apart from when I initially change the state field. All subsequent state changes work as expected, just not the first.

 

Example: I open an incident which is in state New. I select 'In Progress' from the dropdown - State is not mandatory. I then select another state from the drop-down before reselecting 'In Progress' - only then does State become mandatory.

 

I have tested this in both a PDI (Utah) and my organisations dev environment (Utah). Both have the same outcome. I would appreciate some help with this. Many thanks.

 

Script:

 

SN_Client_Script.png

 

 

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

   //Type appropriate comment here, and begin script below
   
   if(newValue == '2' && oldValue == '1'){  

        g_form.setMandatory('comments',true);       
        g_form.setReadOnly('work_notes',true);
        g_form.showFieldMsg('comments','Please enter a customer update, preferably using a suitable template','info');
    }
    else {
        g_form.setMandatory('comments',false);
        g_form.setReadOnly('work_notes',false);
        g_form.hideFieldMsg('comments','Please enter a customer update, preferably using a suitable template','error');

    }
}

 

5 REPLIES 5

That is weird.

I wonder if there is another script running at a latter order or a ui policy that overrides the comments field behavior

-Anurag