onChange script

nmcl
Giga Expert

I've got a script that is working (thanks to http://www.servicenowguru.com/scripting/client-scripts-scripting/javascript-popup-boxes-servicenow/).

But I want this script to query if the priority field is changing to one of three specific values and then only display.

I can make this happen onSubmit, but would prefer this to be an onChange script so that it's flagged straight away.

 

Requirement: if the 'priority' field value changes to 6,7 or 8 the alert appears.

 

--- script ---

 

function onChange(control, oldValue, newValue, isLoading) {

      if (!isLoading)

      alert('Set the Priority as a Major Incident?');

}

1 ACCEPTED SOLUTION

marcguy
ServiceNow Employee
ServiceNow Employee

function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue == '') { //if loading or new value is blank, do nothing


          return;


    }



if (newValue == 6 || newValue == 7 || newValue == 8){ //if new value is 6,7 or 8


alert('Set the Priority as a Major Incident?');


}




if you wanted to ask the user the question here so they could click OK or Cancel you could use a return confirm and actually set the value if they click OK and mark a major incident field on the form as true




if (confirm('Set the Priority as a Major Incident?')){


g_form.setValue('u_major_incident',true);


}


View solution in original post

7 REPLIES 7

marcguy
ServiceNow Employee
ServiceNow Employee

function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue == '') { //if loading or new value is blank, do nothing


          return;


    }



if (newValue == 6 || newValue == 7 || newValue == 8){ //if new value is 6,7 or 8


alert('Set the Priority as a Major Incident?');


}




if you wanted to ask the user the question here so they could click OK or Cancel you could use a return confirm and actually set the value if they click OK and mark a major incident field on the form as true




if (confirm('Set the Priority as a Major Incident?')){


g_form.setValue('u_major_incident',true);


}


Thanks, that did the trick OK and I used the confirm prompt.


One final question on this one, I'd like to add in another query so that if the value changes between 6,7,8 that it doesn't re-apply the template. I'm guessing this would be something like the below but not sure whether this can be a separate query to the newValue query?



if (oldValue != 6 || oldValue != 7 || oldValue != 😎



--- script ---



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue == '') { //if loading or new value is blank, do nothing


          return;


    }



if (newValue == 6 || newValue == 7 || newValue == 😎 //if new value is 6,7 or 8




if (confirm('Set the Priority as a Major Incident?')){


g_form.setValue('u_major_incident',true);


var grTemplate = new GlideRecord('sys_template');


grTemplate.addQuery('name','* Major Incident Template');


grTemplate.query(templateResponse);



function templateResponse(template) {


if (template.next()) {


applyTemplate(template.sys_id);


}


}


}


}


All sorted - thanks all for your help.


For anyone else who may need it the below client script will apply a template to an incident if a value is selected, but will not reapply if changed.




function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue == '') { //if loading or new value is blank, do nothing


          return;


    }


  if (oldValue == 6 || oldValue == 7 || oldValue == 😎 {


  return;


  }


if (newValue == 6 || newValue == 7 || newValue == 😎 //if new value is 6,7 or 8




if (confirm('Set the Priority as a Major Incident?')){


g_form.setValue('u_major_incident',true);


var grTemplate = new GlideRecord('sys_template');


grTemplate.addQuery('name','* Major Incident Template');


grTemplate.query(templateResponse);



function templateResponse(template) {


if (template.next()) {


applyTemplate(template.sys_id);


}


}


}


}