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

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi,

I am assuming you want to validate this based on the snc_internal role?

 

-Pradeep Sharma

Hi @Pradeep Sharma , 

 

I want validate if the table  for each Report if they table has a value in the column. 

For example: 

answer = ifScript();
function ifScript() {

// This table name

var reg = new GlideRecord('u_power_bi_azure_lookup');

//This is the name of the report
reg.addQuery('u_report_name', "Portfolio Metrics Report");

//This is the name of the column, that have empy fields depending of the Report. If an azure ad group is there it generate a task to add the user to an ad group if the field is empty it should skip this step and goes to the next step.

reg.addQuery('u_internal_azure_ad_group', '');
reg.query();
if (reg.next()){
return 'yes';
}
else {
return 'no';
}
}

 

Mike Patel
Tera Sage
Provide more details so we can help you with code.

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!