If condition is not working in Business rules

Sanjeeva Nagend
Tera Contributor

If condition is not working properly.

 

var gritem = new GlideRecord('sc_req_item');
        gritem.addQuery('sys_id', current.sysapproval);
        gritem.query();
        if (gritem.next()) {
           
            var catalog_item1 = gritem.cat_item;
           
            if ((catalog_item1 != 'f6d8b512db4797009a88b1b2399619d4') || (catalog_item1 != '8ce740824f684b00a032ecd18110c7af') || (catalog_item1 != '48bd939a1be1fcd0e3bfa9b1604bcbc9') || (catalog_item1 != '950045221be93014e3bfa9b1604bcb96') || (catalog_item1 != '0174e3021b253cd0e3bfa9b1604bcb21') || (catalog_item1 != '6aef35cc1b4354d0c592dc29cd4bcb28') || (catalog_item1 != '34b2bbde1b32d490c592dc29cd4bcb79') || (catalog_item1 != '1ecd222a1bb61890c592dc29cd4bcb23') || (catalog_item1 != '574687a61b7a1890c592dc29cd4bcb69') || (catalog_item1 != 'b32831481b0354d0c592dc29cd4bcb0d') || (catalog_item1 != 'f7cc79881b4354d0c592dc29cd4bcb2f') || (catalog_item1 != '42dc1c2f1b000d10e3bfa9b1604bcb80') || (catalog_item1 != 'f7cc79881b4354d0c592dc29cd4bcb2f')) { 

                gs.addInfoMessage("Testing approvals");
               }
 
I have added the catalog item sysid to the if condition, but it still is not working.
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Sanjeeva Nagend 

try this and better to use an array

var excludedIds = [
    'f6d8b512db4797009a88b1b2399619d4',
    '8ce740824f684b00a032ecd18110c7af',
    '48bd939a1be1fcd0e3bfa9b1604bcbc9',
    '950045221be93014e3bfa9b1604bcb96',
    '0174e3021b253cd0e3bfa9b1604bcb21',
    '6aef35cc1b4354d0c592dc29cd4bcb28',
    '34b2bbde1b32d490c592dc29cd4bcb79',
    '1ecd222a1bb61890c592dc29cd4bcb23',
    '574687a61b7a1890c592dc29cd4bcb69',
    'b32831481b0354d0c592dc29cd4bcb0d',
    'f7cc79881b4354d0c592dc29cd4bcb2f',
    '42dc1c2f1b000d10e3bfa9b1604bcb80'
];

var gritem = new GlideRecord('sc_req_item');
gritem.addQuery('sys_id', current.sysapproval);
gritem.query();
if (gritem.next()) {
    var catalog_item1 = gritem.cat_item.toString();
    if (excludedIds.indexOf(catalog_item1.toString()) == -1) {
        gs.addInfoMessage("Testing approvals");
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Sanjeeva Nagend 

try this and better to use an array

var excludedIds = [
    'f6d8b512db4797009a88b1b2399619d4',
    '8ce740824f684b00a032ecd18110c7af',
    '48bd939a1be1fcd0e3bfa9b1604bcbc9',
    '950045221be93014e3bfa9b1604bcb96',
    '0174e3021b253cd0e3bfa9b1604bcb21',
    '6aef35cc1b4354d0c592dc29cd4bcb28',
    '34b2bbde1b32d490c592dc29cd4bcb79',
    '1ecd222a1bb61890c592dc29cd4bcb23',
    '574687a61b7a1890c592dc29cd4bcb69',
    'b32831481b0354d0c592dc29cd4bcb0d',
    'f7cc79881b4354d0c592dc29cd4bcb2f',
    '42dc1c2f1b000d10e3bfa9b1604bcb80'
];

var gritem = new GlideRecord('sc_req_item');
gritem.addQuery('sys_id', current.sysapproval);
gritem.query();
if (gritem.next()) {
    var catalog_item1 = gritem.cat_item.toString();
    if (excludedIds.indexOf(catalog_item1.toString()) == -1) {
        gs.addInfoMessage("Testing approvals");
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Sanjeeva Nagend 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

gaurrohi
Tera Expert

Hi @Sanjeeva Nagend 

The if condition in your script is not working as expected because of the use of the OR operator when you intend to check if catalog_item1 is different from all the specified sys_ids. As it is always evaluating to true.
you need to use the AND operator.

var gritem = new GlideRecord('sc_req_item');
gritem.addQuery('sys_id', current.sysapproval);
gritem.query();
if (gritem.next()) {
    var catalog_item1 = gritem.cat_item.toString();

    if (
        (catalog_item1 != 'f6d8b512db4797009a88b1b2399619d4') &&
        (catalog_item1 != '8ce740824f684b00a032ecd18110c7af') &&
        (catalog_item1 != '48bd939a1be1fcd0e3bfa9b1604bcbc9') &&
        (catalog_item1 != '950045221be93014e3bfa9b1604bcb96') &&
        (catalog_item1 != '0174e3021b253cd0e3bfa9b1604bcb21') &&
        (catalog_item1 != '6aef35cc1b4354d0c592dc29cd4bcb28') &&
        (catalog_item1 != '34b2bbde1b32d490c592dc29cd4bcb79') &&
        (catalog_item1 != '1ecd222a1bb61890c592dc29cd4bcb23') &&
        (catalog_item1 != '574687a61b7a1890c592dc29cd4bcb69') &&
        (catalog_item1 != 'b32831481b0354d0c592dc29cd4bcb0d') &&
        (catalog_item1 != 'f7cc79881b4354d0c592dc29cd4bcb2f') &&
        (catalog_item1 != '42dc1c2f1b000d10e3bfa9b1604bcb80')
    ) {
        gs.addInfoMessage("Testing approvals");
    }
}

However, as suggested by @Ankur Bawiskar, the array approach makes it easier to manage the list of sys_ids and for better maintenance of a long list of IDs

Please mark the response as the correct answer and helpful. This may help other community users to follow the correct solution.

Regards,
Rohit Singh

Hristo Ivanov
Kilo Sage

The problem is with your condition logic:

 

if ((catalog_item1 != 'A') || (catalog_item1 != 'B') || ...)

 

This condition will always be true, no matter what the catalog_item1 is. You can use AND(&&) for !=, or use Array.includes()

Option 1:

 

var excludedItems = [
  'f6d8b512db4797009a88b1b2399619d4',
  '8ce740824f684b00a032ecd18110c7af',
  '48bd939a1be1fcd0e3bfa9b1604bcbc9',
  '950045221be93014e3bfa9b1604bcb96',
  '0174e3021b253cd0e3bfa9b1604bcb21',
  '6aef35cc1b4354d0c592dc29cd4bcb28',
  '34b2bbde1b32d490c592dc29cd4bcb79',
  '1ecd222a1bb61890c592dc29cd4bcb23',
  '574687a61b7a1890c592dc29cd4bcb69',
  'b32831481b0354d0c592dc29cd4bcb0d',
  'f7cc79881b4354d0c592dc29cd4bcb2f',
  '42dc1c2f1b000d10e3bfa9b1604bcb80'
];

var gritem = new GlideRecord('sc_req_item');
gritem.addQuery('sys_id', current.sysapproval);
gritem.query();
if (gritem.next()) {
    var catalog_item1 = gritem.cat_item.toString(); // ensure it's a string

    if (!excludedItems.includes(catalog_item1)) {
        gs.addInfoMessage("Testing approvals");
    }
}

 

 

option 2:

 

if (
  catalog_item1 != 'A' &&
  catalog_item1 != 'B' &&
  catalog_item1 != 'C'
) {
  // true only if it's not any of them
}

 

Please mark this response as correct and helpful if it helps you