Query a Table using a If Statement

Edxavier Robert
Mega Sage

I am trying to create a IF statement inside a Workflow to see if the request.requested_for is a Internal employee. 

Any suggestions? 

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

As the others have mentioned, unfortunately you didn't provide much information for us to assist you...but...the code to query a table would look like:

answer = ifScript();

ifScript() {
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', current.request.requested_for);
gr.addQuery('u_internal_employee', 'true');
gr.query();
if (gr.next()) {
return 'yes';
} else {
return 'no';
}
}

So here you would want to look at replacing "u_internal_employee" with the name of the field on the user record where you are tracking if they are internal or not? I just assumed it was called u_internal_employee and it was a checkbox returning "true" meaning it was checked.

If so, the path out of the if statement activity in your workflow would go out of the yes route or if they weren't, it would go out of the no path.

Or if you're trying to look for a specific role, then:

answer = ifScript();

ifScript() {
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', current.request.requested_for);
gr.addQuery('role', 'sys_id_of_role');
gr.query();
if (gr.next()) {
return 'yes';
} else {
return 'no';
}
}

Where you'd want to replace "sys_id_of_role" with the sys_id of that role.

My code above is merely an example to get you started.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

11 REPLIES 11

Edxavier Robert
Mega Sage

Hi All, 

I wanted to validate if one column has an empty row. I have this table called: u_power_bi_azure_lookup that table has 3 columns and I want check if the column u_internal_azure_ad_group has an empty row. 

I have this but is returning No all the time. 

answer = ifScript(); 
function ifScript() { 
var reg = new GlideRecord('u_power_bi_azure_lookup'); 
if (reg.u_internal_azure_ad_group == '') {
return 'yes';

else { 
return 'no'; 

}

Edxavier Robert
Mega Sage

Thanks Allen, I will try that and will let you know. 

answer = ifScript(); 
function ifScript() { 
var reg = new GlideRecord('u_power_bi_azure_lookup'); 
reg.query();
if (reg.u_internal_azure_ad_group == '') {
return 'yes';
} 
else { 
return 'no'; 
} 
}

Hello,

I've added in a reg.query(); to your script..but that is going to query the ENTIRE table...which doesn't really make sense. So refer to my script above about looking at specific record/sys_id? A specific user/sys_id? etc. Something that makes more sense like:

answer = ifScript(); 
function ifScript() { 
var reg = new GlideRecord('u_power_bi_azure_lookup');
reg.addQuery('sys_id', current.variables.user);
reg.query();
if (reg.u_internal_azure_ad_group == '') {
return 'yes';
} 
else { 
return 'no'; 
} 
}

So adding something like:

reg.addQuery('sys_id', current.variables.user);
reg.query();

so that you're adding additional parameters to the query to narrow it down to a specific use case.

https://www.servicenowguru.com/scripting/gliderecord-query-cheat-sheet/

Please mark reply as Helpful/Correct, if applicable. Thanks!

 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Edxavier Robert
Mega Sage

Hi Allen, 

Yes, I will need to query the entire table because I will need to look if the column u_internal_azure_ad_group has empty rows. I tried your code but is giving me an error of the sysid lline, so I changed to this:

answer = ifScript();
function ifScript() {
var reg = new GlideRecord('u_power_bi_azure_lookup');
reg.addQuery('u_internal_azure_ad_group');
reg.query();
if (reg.u_internal_azure_ad_group == '') {
return 'yes';
}
else {
return 'no';
}
}

 

But still returning NO, 

 

Let me give you more information: I currently working in a workflow for an access request, if the user is a internal user and they will need to skip the step to add this user to the azure AD group. The query will need to determinate if the column has an empty row. Let me know if you need more information and thanks for you help on this. 

Edxavier,

Ok...

answer = ifScript();

function ifScript() {
var reg = new GlideRecord('u_power_bi_azure_lookup');
reg.addQuery('u_internal_azure_ad_group', '');
reg.query();
if (reg.next()){
return 'yes';
}
else {
return 'no';
}
}

So the above would query the table and look for any record that has a blank/empty value for u_internal_azure_ad_group, if so, it'll return yes, if not, it'll return no.

Just mentioning...again...that this is basically going to look at that table...and look for ANY RECORD that has a blank/empty value for that field.

It is NOT querying the table...looking at a specific record like the "Overview Report" as seen in your screenshot...and then look at the azure ad group field for a blank/empty value.

If you needed to look at, for example, a specific record, as I mentioned above, you would need to narrow down your query with another addQuery('u_report_name', "Name of Report"); type line.

Please mark reply as Helpful/Correct, if applicable. Thanks!

 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!