IF statement always returning true.

Venjamin
Tera Contributor

So, I have a fairly basic script, that should, in theory, be giving me one specific answer. I'm looking for it to find "Snowflake - HCDA PII Prod" and ensure that the PII Approval checkbox is true. I have tried removing the second feature - the name should be enough. 

 

For some reason, though, it always seems to be returning yes. I feel like I am overlooking something really basic, and I'm going to feel incredibly goofy. I tried with double quotes and single, I tried changing it up to search for the sys_id instead of the name. I tried swapping to an encoded query. It's just always delivering 'yes.' Please someone take a look and put me out of my misery? 

 

answer = ifScript();

function ifScript() {

var catvar = new GlideRecord("u_catalog_item_variables_list");
//catvar.get(obj.mrvs_al);
catvar.addQuery('u_name', 'Snowflake - HCDA PII Prod');
catvar.addQuery('u_needs_pii_approval', true);
catvar.query();
		gs.log('VVVVVV info reference- ' + current.variables.sar_system);
		gs.log('VVVVVV info NPA- ' + catvar.u_needs_pii_approval);
if (catvar.next()) {
return 'yes';
}
1 ACCEPTED SOLUTION

Vishal Birajdar
Giga Sage

Hi @Venjamin 

If it's returning as yes that means record with that query is present in that table.

Can you check if record is present in the table.

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

6 REPLIES 6

Marcos Kassak
Kilo Sage
Kilo Sage

Hi @Venjamin,

 

Your script seems to be logically correct, but there are a few things that you might want to check or modify to debug the issue, lets breakdown into steps:

 

1. Logging
You are logging catvar.u_needs_pii_approval before calling catvar.next(). This means that it will not have any value at that point. You should log it after calling catvar.next().

 

2. Debugging
Add more logging to see if the query is returning any records and to check the values of the fields in those records.

 

3. Check the Field Type
Ensure that u_needs_pii_approval is a boolean field. If it is a string or another type, you may need to adjust your query accordingly.

 

4. Check the Field Values
Could you make sure that there are records in your instance where u_name is 'Snowflake - HCDA PII Prod' and u_needs_pii_approval is true?

 

Modified Script


Here’s a modified version of your script with added logging for debugging:

 

answer = ifScript();

function ifScript() {
    var catvar = new GlideRecord("u_catalog_item_variables_list");
    catvar.addQuery('u_name', 'Snowflake - HCDA PII Prod');
    catvar.addQuery('u_needs_pii_approval', true);
    catvar.query();
    
    gs.log('VVVVVV info reference- ' + current.variables.sar_system);
    
    if (catvar.hasNext()) {
        catvar.next();
        gs.log('VVVVVV Record Found. u_needs_pii_approval value: ' + catvar.u_needs_pii_approval);
        return 'yes';
    } else {
        gs.log('VVVVVV No Record Found');
        return 'no'; // or whatever you want to return if no record is found
    }
}

 

If there are no records in the u_catalog_item_variables_list table that meet the criteria, the script will always return 'yes' as per your current logic. Ensure that there are records that meet the criteria, and if not, you may want to adjust the return value when no records are found.

 

Run it again and let me know about the outcome!

 

 

If you found my answer helpful or correct in any way, please don't forget to mark it to help future readers! 👍

 

--

 

Kind regards,


Marcos Kassak
Solution Consultant  🎯

 

Marcos,

Thanks for the information. So far, it's returning the incorrect information: 

 

Venjamin_0-1695417933320.png

So it perceives the Life PII prod, but it's suggesting that it's a "yes." 

On the other side, when I ran against HCDA PII Prod, it still came back as yes, after perceiving the correct info. 

 

Venjamin_1-1695418102240.png

 

Anurag Tripathi
Mega Patron
Mega Patron

Try this

answer = ifScript();

function ifScript() {

var catvar = new GlideRecord("u_catalog_item_variables_list");
//catvar.get(obj.mrvs_al);
catvar.addQuery('u_name', 'Snowflake - HCDA PII Prod');
catvar.addQuery('u_needs_pii_approval', true);
catvar.query();
		gs.log('VVVVVV info reference- ' + current.variables.sar_system);
		gs.log('VVVVVV info NPA- ' + catvar.u_needs_pii_approval);
if (catvar.hasNext()) {
return 'yes';
}
else{
return 'no';
}
-Anurag

I appreciate you taking a look, unfortunately it is running into the same issue.