Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Incident Priority Client Script

CandyDee
Kilo Sage

Working on a client script that if a P1 or P2 incident is logged, if its down graded to to a P3, P4 or P5 after its been submitted an alert pop ups querying if its correct to downgrade its priority.

 

Currently the script i have is below but need to add some more to it, the alert appears when flipping between p2 and p1 priorities but i need it to appear ONLY when deescalting the priority going from P1 to p2 to p3 to p4 to p5.

Thanks

 

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

//Type appropriate comment here, and begin script below
var priority = g_form.getValue('priority');
if (priority <= 2) {
alert(getMessage('Are you sure you want to de-escalate this incident?'));
return false;
}
}

1 ACCEPTED SOLUTION

JP52
Mega Expert

Hey CandyDee,

In your code, the < needs to be a >. The Priority is being lowered, but the value of the lower priorities is a greater integer. I.e. 5 - Planning is > 2 - High

 

To have the alert appear if the Priority drops from either P1 or P2 to P3, P4 or P5 (but not when lowering from less than P2), try this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	if ((newValue > 2 && oldValue <= 2)){
		alert('Are you sure you want to de-escalate this incident?');
		}
}

 

If you'd like to enhance it a bit and change the alert to a prompt where if the users hits cancel, the form will revert to the original values when the form was loaded, try this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	var oldUrgency = '';
	var oldImpact = '';
	
	//get the current record values from the incident table
	var incNum = g_form.getValue('number');
	var gr = new GlideRecord('incident');
	gr.addQuery('number', incNum);
	gr.query();
	while(gr.next()) {
		//alert(gr.number);
		oldUrgency = gr.urgency;
		oldImpact = gr.impact;
	}
	
	if (newValue > 2 && oldValue <= 2){
		
		var answer = confirm('Are you sure you want to de-escalate this incident?');
		if (!answer) {
			//If user cancels, revert the values
			g_form.setValue('priority', oldValue);
			g_form.setValue('urgency', oldUrgency);
			g_form.setValue('impact', oldImpact);
		}
		return false;
	}
}

 

In both options however, the user will get two prompts/ alerts if both the Impact and Urgency are changed.

 

Cheers.

View solution in original post

5 REPLIES 5

Anurag Tripathi
Mega Patron
Mega Patron

Why dont you use an confirm box instead of alert?

 

if (isLoading || newValue === '') {
return;
}

//Type appropriate comment here, and begin script below
var priority = g_form.getValue('priority');
if (priority <= 2) {
var r = condirm(getMessage('Are you sure you want to de-escalate this incident?'));

if(!r)
return false;
}
}

 

-Anurag

-Anurag

Uma2
Tera Expert

Hi CandyDee,

try this one,actually on change with out saving the form the oldvalue not updated.

 

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


var priority = g_form.getValue('priority');
g_form.addInfoMessage('value is'+newValue);
if (newValue>2) {
alert(getMessage('Are you sure you want to de-escalate this incident?'));

}
}

ggg
Giga Guru

In the Onchange client script

you have the oldValue and the newValue

so you can compare.

if you are de-escalating

    show a message.

JP52
Mega Expert

Hey CandyDee,

In your code, the < needs to be a >. The Priority is being lowered, but the value of the lower priorities is a greater integer. I.e. 5 - Planning is > 2 - High

 

To have the alert appear if the Priority drops from either P1 or P2 to P3, P4 or P5 (but not when lowering from less than P2), try this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	if ((newValue > 2 && oldValue <= 2)){
		alert('Are you sure you want to de-escalate this incident?');
		}
}

 

If you'd like to enhance it a bit and change the alert to a prompt where if the users hits cancel, the form will revert to the original values when the form was loaded, try this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	var oldUrgency = '';
	var oldImpact = '';
	
	//get the current record values from the incident table
	var incNum = g_form.getValue('number');
	var gr = new GlideRecord('incident');
	gr.addQuery('number', incNum);
	gr.query();
	while(gr.next()) {
		//alert(gr.number);
		oldUrgency = gr.urgency;
		oldImpact = gr.impact;
	}
	
	if (newValue > 2 && oldValue <= 2){
		
		var answer = confirm('Are you sure you want to de-escalate this incident?');
		if (!answer) {
			//If user cancels, revert the values
			g_form.setValue('priority', oldValue);
			g_form.setValue('urgency', oldUrgency);
			g_form.setValue('impact', oldImpact);
		}
		return false;
	}
}

 

In both options however, the user will get two prompts/ alerts if both the Impact and Urgency are changed.

 

Cheers.