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

Hello @abirakundu23 

 

Then you can update condition to exclude the execution if state is in progress:

 

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

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 ,

Regarding this request instead of adding this condition : if (tickettype == "871c5d5e87b3e9140811646e8bbb3530" && approv == "not requested" && g_form.getValue("state") != "In progress") to if (tickettype == "871c5d5e87b3e9140811646e8bbb3530" && approv == "not requested" && g_form.getValue("state") == "Resolved") not working properly after if loop, within if loop alert not display. could you please help me on that.

 

 
 
   //Type appropriate comment here, and begin script below
 
 
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");
}
}
 
 
}

SwarnadeepNandy
Mega Sage

Hello,

 

One Probable reason your alert is showing as a loop is that you are using an onChange client script, which means that every time the state field changes, your script will run again. This can create an infinite loop if your script changes the state field back to “In progress”. To avoid this, you can use a condition to check if the state field is already “In progress” before running your script. For example:

if (newValue != ‘In progress’) { // your script logic here }

 

Hope it helps.

 

Kind Regards,

Swarnadeep Nandy

Hi @SwarnadeepNandy,

 

Can you please help me on that ??

Can you please provide some screenshots?