How to create an error message using client script

Raphael Dizon
Mega Expert

Hello,

I have a task wherein I have to create an error message using client script. The scenario is whenever a user changes the state of the incident to resolve and an incident task or job order is still open, it will display an error message which says "Please Close all incident tasks" or "Please Close all Job orders" after submitting the incident. Thank you.

Regards,

Raphael

1 ACCEPTED SOLUTION

Service_RNow
Mega Sage

HI,

Please write a Business rule on the INcident table 

When to run ->Before,Update

Action-> state To close

Advance code:

(function executeRule(current, previous /*null when async*/) {

// Add your code here
var gr = new GlideRecord('incident_task);

gr.addQuery('active','true');

gr.addQuery('incident',current.sys_id);

gr.query();

if (gr.next()) {

gs.addErrorMessage('Incident cannot be closed before the incident task closure');

current.setAbortAction(true);

}

})(current, previous);

 

Please mark reply as Helpful/Correct, if applicable. Thanks!

View solution in original post

12 REPLIES 12

Hardik Benani
Mega Sage
Mega Sage

You can write a OnChange, OnSubmit client script which will check if there are any open incident and use below

 https://docs.servicenow.com/bundle/london-application-development/page/script/useful-scripts/reference/r_DisplayFieldMessages.html

I think there is an OOTB Business rule that already checks this. You can also use below onBefore Business Rule with condition state changes to resolved.

 

(function executeRule(current, previous /*null when async*/) {

checkIncidentTaskState(current);

function checkIncidentTaskState(me) {
var incident = new GlideRecord('incident_task');
incident.addQuery('incident', me.sys_id);
incident.addQuery('state', 'NOT IN', '3,4,7');
//incident.addActiveQuery();
incident.query();
if (incident.next()) {
var msg = gs.getMessage("Please close all Incident Tasks before closing the incident");
gs.addErrorMessage(msg);
current.setAbortAction(true);
current.setWorkflow(false);
}
}


//resolveIncidentTasks(current);

function resolveIncidentTasks(me) {
var incident = new GlideRecord('incident_task');
incident.addQuery('incident', me.sys_id);
incident.addActiveQuery();
incident.query();
while (incident.next()) {
var msg = gs.getMessage("Incident Tasks '{0}' closed based on closure of task '{1}'", [incident.number, me.number]);
incident.state = '3';
incident.active.setValue(false);
incident.comments = msg;
incident.update();
}
}

})(current, previous);

 

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

Hi,

I already have a business rule for aborting the update whenever an incident is resolved and an incident task is still open because in our incident task we have an auto-resolve of incident business rule whenever an incident task is closed but I want to separate the error message by using a client script so that the error message doesn't show in the incident task form. 

Service_RNow
Mega Sage

Hi,

Business rule and other general use scripts
ScriptResult
current.field_name.setError("Hello World");Will put "Hello World" below the specified field.
gs.addInfoMessage("Hello World");Will put "Hello World" on the top of the screen.
gs.print("Hello World");Will write to the text log on the file system but not to the sys_log table in the database.
gs.log("Hello World");Will write to the database and the log file.
Note: Too much of this can adversely affect performance.
Important: The methods in this table are only for use in client scripts.
Client side scripts
ScriptResult
alert("Hello World");Will pop up a window with "Hello World" and an 'OK' button.
confirm("Hello World");Will pop up a window with "Hello World?" and a 'Ok' and 'Cancel' buttons.
g_form.showFieldMsg("field_name", "Hello World", "error");Puts "Hello World" in an error message below the specified field.
g_form.hideFieldMsg("field_name");Hides an error message that is visible under the specified field.

Please mark reply as Helpful/Correct, if applicable. Thanks!

Hi,

Can you use GlideRecord on a client script?