Incident state is updating even though added if condition

Vinodh Kumar
Tera Contributor

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);

1 ACCEPTED SOLUTION

Iraj Shaikh
Mega Sage
Mega Sage

Hi @Vinodh Kumar 

 

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.

View solution in original post

3 REPLIES 3

Iraj Shaikh
Mega Sage
Mega Sage

Hi @Vinodh Kumar 

 

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.

Hi @Iraj Shaikh


It worked, Thank you for the solution and the explanation.

Ehab Pilloor
Mega Sage

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