please tell me script right or wrong , if wrong pls correct i

venkyc
Kilo Contributor

31. Prevent change to a specific value
Field: State
Script: if(newValue == 'Closed'){ alert('You cannot directly set state to Closed');
g_form.setValue('state', oldValue); }
Explanation: Enforces process rules.


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

if (newValue === 'Closed') {
  g_form.showFieldMsg('state','Cannot close directly. Please follow the proper workflow.' );
  g_form.setValue('state', oldValue);
}
3 REPLIES 3

Bhavya11
Kilo Patron

Hi @venkyc ,

 

if the state field value Closed is Closed then it will work but there is issue with your script it wont show the field message properly.

 

as per the your code it directly set the value not show any message under the 'state' field.

 

 g_form.showFieldMsg('state','Cannot close directly. Please follow the proper workflow.' );
  g_form.setValue('state', oldValue);
 
 
This also applies when using g_form.setValue().  g_form.showFieldMsg() must come after the call to g_form.setValue() for the same field.
 
if you want to show the field message try something like this 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '' ||newValue === oldValue) {
      return;
   }

if (newValue === '7') { // '7' is the "Closed" state 'change the value '7' to 'Closed'
        g_form.hideFieldMsg('state');

        g_form.setValue('state', oldValue);

        setTimeout(function() {
            g_form.showFieldMsg('state', 'Cannot close directly. Please follow the proper workflow.', 'error');
        }, 100);  
}}

 

 

If this information proves useful, kindly mark it as helpful or accepted solution.

Thanks,
BK

Ankur Bawiskar
Tera Patron

@venkyc 

what's your business requirement?

you are restricting Closed state using onChange script

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

adityahubli
Tera Guru

Hello @venkyc ,

I just want to clarify which table this client script is applied to.

If this script is applied on the Incident table, then the Closed state has a backend value of 7, so the condition newValue == '7' should work for displaying the error message.

However, please note that Incident already has OOTB data policies that enforce mandatory fields like Close Notes and Close Code when moving the state to Closed.

If this script is being used on any other table, that table may have a different backend value for the Closed (or equivalent) state. In that case, you should verify the backend value of the state choice on that table and compare that value with newValue in the script.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   if(newValue=='2')
   {
    g_form.addErrorMessage("You can not chnage the state")
   }

   //Type appropriate comment here, and begin script below
   
}
 
If this helps you then mark it as helpful and accept as solution.
Regards,
Aditya