The CreatorCon Call for Content is officially open! Get started here.

How to check a condition in addEncodedQuery()

Ajith7
Tera Contributor

I want to send a notification to users who are inactive for 25 days and the role should be itil and the group should not be custom group(group name). I have checked the condition for role but I could not put a condition to check the group name in addEncodedQuery(). Can anyone resolve this issue.

 

gr.addEncodedQuery("roles=itil^active=true^last_login_timeRELATIVELE@dayofweek@ago@1");

 

I wanted to put a condition for group

eg:

   group not equal to test_group.

Can anyone help me to put the correct condition

1 ACCEPTED SOLUTION

How are you using the result? If you want to use the user's sys_id remember that when you query the sys_user_grmember table then if you will use gr.sys_id it will give you the sys_id of a connection record so it will be not the User's sys id.

To use the user's sys_id you should do gr.user.sys_id

 

In overall if you need to access user fields when you query sys_user_grmember table you need to dot walk to this field.

 

e.g.

var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery('user.last_loginRELATIVELT@dayofweek@ago@25^user.roles=admin^user.active=true^group.name!=Team Development Code Reviewers1');
gr.query();

while (gr.next()){
 gs.print(gr.user.sys_id); // print the user's sys_id
}

View solution in original post

26 REPLIES 26

Hi Ajith,

Can you share the complete code?

Regards

Ankur

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

var gr = new GlideRecord("sys_user_grmember");
gr.addEncodedQuery("user.roles=itil^group.name!=Test_Group1^user.active=true^user.last_login_timeRELATIVELE@dayofweek@ago@1");
gr.query();

while (gr.next()) {
gs.eventQueue("ServiceNow.Deactivation.Notification", current, gr.sys_id, gr.manager);
}

Hi Ajith,

can you try to add log to know if any record satisfying that condition is present or not

this was not working I guess since gr is now sys_user_grmember object and you are picking up manager and sys id of that; I assume you want email to be sent to user and it's manager

try{
var gr = new GlideRecord("sys_user_grmember");
gr.addEncodedQuery("user.roles=itil^group.name!=Test_Group1^user.active=true^user.last_login_timeRELATIVELE@dayofweek@ago@1");
gr.query();
gs.info('RowCount is: ' + gr.getRowCount());
while (gr.next()) {
// this was not working since gr is sys_user_grmember object and you are picking up manager and sys id of that; I assume you want email to be sent to user and it's manager
var emailArr = [];
emailArr.push(gr.user.toString());
emailArr.push(gr.user.manager.toString());
 gs.eventQueue("ServiceNow.Deactivation.Notification", current, emailArr);
}
}
catch(ex){
gs.info('Exception is: ' + ex);
}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

What exactly I wanted is I want to send a notification to the user if he is inactive for 25 days. The following Two conditions I have to Check . 1. If the user has ITIL role. 2 . If user is a part of Test_Group1(my group) Then I should not send the notification to the user.

Hi Ajith,

are you not getting the rowCount as per the above script? Is the script not working? what issue you are facing?

the notification will trigger only when matching record is found

Regards

Ankur

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