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

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

//Type appropriate comment here, and begin script below
var tickettype = g_form.getValue('ticket_type');
alert(tickettype);
var approv = g_form.getValue('approval');
alert(approv);
var st = g_form.getValue("state");
alert(st);

if (tickettype == "871c5d5e87b3e9140811646e8bbb3530" && approv == "not requested" && g_form.getValue("state") == "Resolved")

{
alert("Hello world");
g_form.addErrorMessage("Status should not change");
g_form.setValue("state","In progress");
alert("Hello world123");
}

}

Initially it was worked for when we put 3rd condition g_form.getValue(state) != "In Progress" but some requirement we want to change g_form.getvalue(state) =="Resolved" but within if loop no alert is working. can you help me on that ??

Hi @SwarnadeepNandy,

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 (tickettype == "871c5d5e87b3e9140811646e8bbb3530" && approv == "not requested" && g_form.getValue("state") == "Resolved")
 
{
alert("Hello world");
g_form.addErrorMessage("status could not change");
g_form.setValue("state","In progress");
alert("Hello world123");
}
}