- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2022 06:47 PM
Hi.
I previously posted a question in the community and solved it.
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 two problems.
- How can I remove the "Invalid update" in the image?
- 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 07:23 AM
Hi,
Understood. That at least confirms why I was suggesting business rule in your other thread, so I just wanted to make sure.
With that said, going back to business rule, the abort action will result in that "Invalid update" error message. You would have to customize and make a change to the platform to stop that from happening and I don't know if you'd want to do that, I wouldn't recommend it.
With that said, then it comes down to optimizing your code, right? Where you're basically wanting to supply a list of fields that should contain a value and if empty, include that in your error message?
So instead of manually typing then line by line and the script getting bigger and bigger, you wanted to do it more streamlined, correct?
With that said, you can try this:
var fields = ['business_service', 'cmdb_ci'];
var eFields = [];
for (var i = 0; i < fields.length; i++) {
if (current.getValue(fields[i]) == '') {
eFields.push(current[fields[i]].getLabel());
}
}
gs.addErrorMessage("please fill in the value for these fields:" + eFields);
So here, all you would need to do is add the field name to the "fields" array at the top of the script and then it'll loop through those, check if they're empty, if so, add it to another array that's used when displaying the error message to tell the user to fill it in.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2022 07:31 PM
Hi,
The business rule abort action results in the "Invalid update" error message. This is by default within the platform.
If your requirement is to check that a value is in both the Test and Dev fields if the state is being changed to resolved, then you can forget about the business rule and instead use a UI Policy where the condition is state is resolved.
Then, in the UI Policy Actions related list on that UI Policy, you can set the Dev and Test fields to mandatory = true.
Then if the user tries to change the state to 'Resolved' manually or...uses the "Resolve" button on the form, it'll check to make sure those two fields have a value and if not, auto prompt for the values to be filled in and prevent the save/submission until they do so.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2022 07:47 PM
very sorry.
That method cannot be used.
The requirements I have are very complicated, and ACL (Write) is set in the "DEV" and "TEST" fields.
The reason is that I want a specific user to enter it, but if I operate it as a user who does not have a role, the above UI Policy will not work.
That's why I try to prevent it with the Business Rule.
Therefore, is it difficult to achieve the requirements in a manner that complies with the above Business Rule?
Thank you!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2022 07:23 AM
Hi,
Understood. That at least confirms why I was suggesting business rule in your other thread, so I just wanted to make sure.
With that said, going back to business rule, the abort action will result in that "Invalid update" error message. You would have to customize and make a change to the platform to stop that from happening and I don't know if you'd want to do that, I wouldn't recommend it.
With that said, then it comes down to optimizing your code, right? Where you're basically wanting to supply a list of fields that should contain a value and if empty, include that in your error message?
So instead of manually typing then line by line and the script getting bigger and bigger, you wanted to do it more streamlined, correct?
With that said, you can try this:
var fields = ['business_service', 'cmdb_ci'];
var eFields = [];
for (var i = 0; i < fields.length; i++) {
if (current.getValue(fields[i]) == '') {
eFields.push(current[fields[i]].getLabel());
}
}
gs.addErrorMessage("please fill in the value for these fields:" + eFields);
So here, all you would need to do is add the field name to the "fields" array at the top of the script and then it'll loop through those, check if they're empty, if so, add it to another array that's used when displaying the error message to tell the user to fill it in.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!