- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2022 12:59 AM
Hi.
I previously posted a question in the community and solved it.
https://community.servicenow.com/community?id=community_question&sys_id=88
3c13afdbbe0110770be6be13961978
But there are additional requirements.
The following script now displays an error message indicating that there is an empty field.
The function itself is working, but there are one problems.
- At the moment, only DEV and TEST are available, but future requirements may increase the number of target fields. If the number of fields increases, I think it will be difficult with the current description method. So if you have smarter coding, could you please tell me?
Business Rule:(OnBefore)
Filter Condition:「State」「changes to」「Resolved」
「TEST」「is empty」or「DEV」「is empty」
(function executeRule(current, previous /*null when async*/ ) {
current.incident_state = previous.incident_state;
current.state = previous.state;
current.setAbortAction(true);
gs.clearMessage();
if (gs.nil(current.u_dev) && !gs.nil(current.u_test)) {
gs.addErrorMessage("please fill in the value:DEV");
} else if (!gs.nil(current.u_dev) && gs.nil(current.u_test)) {
gs.addErrorMessage("please fill in the value:TEST");
} else if (gs.nil(current.u_dev) && gs.nil(current.u_test)) {
gs.addErrorMessage("please fill in the value:DEV, TEST");
}
})(current, previous);
Thank you!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2022 01:44 AM
You can consider doing something like this. You just need to fields array in future for adding new environments.
var fields = [{
"fieldName": "u_dev",
"fieldMsg": "Dev"
},
{
"fieldName": "u_test",
"fieldMsg": "Test"
}
];
var missingFields = [];
for (var oField in fields) {
if (gs.nil(current[fields[oField].fieldName])) {
missingFields.push(fields[oField].fieldMsg);
}
}
if (missingFields) {
gs.addErrorMessage("Please fill in the value:" + missingFields.join(", "));
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2022 01:44 AM
You can consider doing something like this. You just need to fields array in future for adding new environments.
var fields = [{
"fieldName": "u_dev",
"fieldMsg": "Dev"
},
{
"fieldName": "u_test",
"fieldMsg": "Test"
}
];
var missingFields = [];
for (var oField in fields) {
if (gs.nil(current[fields[oField].fieldName])) {
missingFields.push(fields[oField].fieldMsg);
}
}
if (missingFields) {
gs.addErrorMessage("Please fill in the value:" + missingFields.join(", "));
}