- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2024 05:37 PM
when this BR runs still closed and resolved Incidents are updating, please let me know what is wrong with this script
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
//gs.addInfoMessage('BR is running');
var inc = new GlideRecord('incident');
inc.addQuery('problem_id', current.sys_id);
inc.query();
while (inc.next()) {
if (inc.state != 6 || inc.state != 7) {
inc.setValue('state', 6);
inc.setValue('close_code', 'Resolved By Problem');
inc.setValue('close_notes', current.fix_notes);
inc.update();
}
}
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2024 05:45 AM - edited ‎01-11-2024 05:47 AM
The issue with the script is in the condition used within the `if` statement:
if (inc.state != 6 || inc.state != 7) {
This condition will always evaluate to `true` because `inc.state` cannot be both 6 and 7 at the same time. Therefore, even if `inc.state` is 6 (Resolved) or 7 (Closed), the condition will pass because one of the two comparisons will always be true.
To fix this, you should use the logical AND operator (`&&`) instead of the logical OR operator (`||`). The correct condition should be:
if (inc.state != 6 && inc.state != 7) {
This way, the code inside the `if` block will only execute if the incident is neither Resolved (6) nor Closed (7).
Here is the corrected script:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
//gs.addInfoMessage('BR is running');
var inc = new GlideRecord('incident');
inc.addQuery('problem_id', current.sys_id);
inc.query();
while (inc.next()) {
if (inc.state != 6 && inc.state != 7) {
inc.setValue('state', 6);
inc.setValue('close_code', 'Resolved By Problem');
inc.setValue('close_notes', current.fix_notes);
inc.update();
}
}
})(current, previous);
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2024 05:45 AM - edited ‎01-11-2024 05:47 AM
The issue with the script is in the condition used within the `if` statement:
if (inc.state != 6 || inc.state != 7) {
This condition will always evaluate to `true` because `inc.state` cannot be both 6 and 7 at the same time. Therefore, even if `inc.state` is 6 (Resolved) or 7 (Closed), the condition will pass because one of the two comparisons will always be true.
To fix this, you should use the logical AND operator (`&&`) instead of the logical OR operator (`||`). The correct condition should be:
if (inc.state != 6 && inc.state != 7) {
This way, the code inside the `if` block will only execute if the incident is neither Resolved (6) nor Closed (7).
Here is the corrected script:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
//gs.addInfoMessage('BR is running');
var inc = new GlideRecord('incident');
inc.addQuery('problem_id', current.sys_id);
inc.query();
while (inc.next()) {
if (inc.state != 6 && inc.state != 7) {
inc.setValue('state', 6);
inc.setValue('close_code', 'Resolved By Problem');
inc.setValue('close_notes', current.fix_notes);
inc.update();
}
}
})(current, previous);
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2024 05:57 AM
Hi @Iraj Shaikh
It worked, Thank you for the solution and the explanation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2024 05:50 AM
Hi @Vinodh Kumar,
You need to make it && instead of || so that your code works properly.
If you found this reply useful, please mark it as solution/helpful.
Thanks and Regards,
Ehab Pilloor