On-change Client script in Loop in Servicenow

abirakundu23
Mega Sage

Hi All,

I wrote one client script on change based on the if condition status could not be change & its will through info/error message & status will back to "in progress" state instead of changing any status.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
 
   //Type appropriate comment here, and begin script below
var ticket = g_form.getValue('type');
alert(ticket);
var approv = g_form.getValue('approval');
alert(approv);
if (ticket == "871c5d5e87b3e9140811646e8bbb3530" && approv == "not requested")
 
{
alert("Hello world");
g_form.addErrorMessage("status could not change");
g_form.setValue("state","In progress");
alert("Hello world123");
}
}

 

Info/Error message not showing & alert is showing as a loop

11 REPLIES 11

Ahmmed Ali
Mega Sage

Hello @abirakundu23 

 

Try modifying below line 

if (tickettype == "871c5d5e87b3e9140811646e8bbb3530" && approv == "not requested")

to 

if (tickettype == "871c5d5e87b3e9140811646e8bbb3530" && approv == "not requested" && oldValue != newValue)

 

Thanks,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Hi @Ahmmed Ali ,

Its not working after adding the condition. Its working same like going on loop.

Hello @abirakundu23 

 

One simple solution which does not require any code would be to have an UI policy created with your condition and make the state field read-only. 

You can also add the field message for the state field saying state cannot be changed.

 

Thank you,

Ali

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Hi @Ahmmed Ali ,

Actually we made read only on status but assigned to not able to change any status that should be a blocker.