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

sudharsanv36
Kilo Guru

Hi Mclaren, You can achieve this by setting up the conditions for the execution of script. You can make the condition field visible in the form by modifying the form layout.



Regards,


Sudharsanan


Hi Sudharasan, I don't seem to be able to get the condition/onclick to be applying the rule.


I have now furthered by script (joining it with another that was already working).



From reading..   Client Scripts - ServiceNow Wiki


I understand that the condition field cannot be used with client scripts (except for onCellEdit()


I now have the below but still need this to only apply if the priority is changed to a value of 6,7 or 8.



So, value of 6,7 or 8 selected - prompt OK/Cancel message.


OK = apply template


Cancel = do nothing



--- script ---


function onCellEdit(sysIDs, table, oldValues, newValue, callback) {


  var   response = confirm('Set the Priority as a Major Incident?');


      if (!response)


          return false;



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);


}


}


}


marcguy
ServiceNow Employee
ServiceNow Employee

what you should be able to do in an OnChange client script is ask for the newValue i.e.



if (newValue == 6 || newValue == 7 || newValue == 8){


Thanks, where abouts would you paste this line in - I keep getting errors?