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

Karthiga S
Kilo Sage

Hi @Lee Jones 

 

Try the same logic with UI Policy. There is an option called "Reverse if false" to be checked to prevent the issue from happening.

Setting mandatory and read-only can be achieved through UI Policy Actions. However, we can control the field messages with UI Policy scripts(refer to the image).

KarthigaS_0-1692020839341.png

 

Please Mark this correct & Hit Like if this solution Works!

 

Regards,

Karthiga

 

Hi Karthiga, thanks for your reply. I have not used UI Policy to achieve this as I need to set an onChange condition, which I can't seem to see here. The script needs to apply when the form is opened in a New state, and is then changed to In Progress. Please advise if this is possible through UI Policy. Thanks.

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Your old value doesn't change after the from is loaded, unless you save the form again.

I'd suggest you run the below and show what alerts you get the first time and the second time you change the state without saving the form

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

   //Type appropriate comment here, and begin script below
   alert('newValue  = '+newValue );
alert('oldValue  = '+ oldValue );
   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');

    }
}
-Anurag

Hi @Anurag Tripathi , thanks for your response. I understand that the oldValue will remain the same as per the last time the record was saved. I want the script to apply exclusively when an incident record is opened (last saved) in a 'New' (state 1) and changed in the form to 'In Progress' (state 2). The script works correctly, but only when you change the state in the open record more than once. 

 

Interestingly, if I keep the script the same, but change 'comments' for another field, e.g. 'business_service', it works correctly, every time. It seems like fails on the first change only when selecting 'comments' as the mandatory field.

 

I've attached 2 videos, the first to demo the incorrect behaviour, the second showing a change to the script from 'comments' to 'business_service' and how this gives the intended behaviour, albeit on another field.

 

Advice would be welcomed.