- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-15-2018 12:14 PM
Hi,
I have written a Scheduled Job which fires an event and that event triggers a notification. I want to add a condition in that Scheduled Job which makes this Scheduled Job trigger that event only when the user has itil role and is part of "XYZ" company. SO, finally the notification has to be sent only to itil users of "XYZ" company.
Conditions when this Scheduled Job should fire the event:
1. User should be "itil" and from company "XYZ"
Below is the screenshot of Scheduled Job:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-15-2018 12:35 PM
The simplest way would be to add an encoded query to your sys_user GlideRecord object. You can generate the query using this method - Generate an encoded query string through a filter . Generate the query by looking at your User table and adding the following filter:
Then add the following line after your first line:
userNoTimeCard.addEncodedQuery("active=true^roles=itil^company=whatever_sys_id_of_tjhe_company");
That will then narrow your list of users down right from the start.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-15-2018 12:35 PM
The simplest way would be to add an encoded query to your sys_user GlideRecord object. You can generate the query using this method - Generate an encoded query string through a filter . Generate the query by looking at your User table and adding the following filter:
Then add the following line after your first line:
userNoTimeCard.addEncodedQuery("active=true^roles=itil^company=whatever_sys_id_of_tjhe_company");
That will then narrow your list of users down right from the start.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-15-2018 12:45 PM
Hi Jim,
Thanks for the response.
I have constructed the filter and got the encoded query as below:
company.nameLIKEAlphaserve^active=true^roles=itil
Basically, I filtered for active itil users of the company whose name contains "Alphaserve".
Is this right or should I specify the company sys_id?
Please let me know.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-15-2018 12:48 PM
That will work, as long as the company name never changes. Using the sys_id allows the name to change, but still points to the same record.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-15-2018 12:54 PM
But does the roles field always stores the data correctly and reliable?
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-15-2018 01:21 PM
To be safe, using the following chunk of code would be the most reliable:
(function() {
//get all the active Alphaserve users
var userNoTimeCard = new GlideRecord('sys_user');
userNoTimeCard.addEncodedQuery('company.nameLIKEAlphaserve^active=true');
userNoTimeCard.query();
while (userNoTimeCard.next()) {
//check to see if the user has the "itil" role
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', userNoTimeCard.getValue("sys_id"));
gr.addQuery('role.name','itil');
gr.query();
if (gr.next()){
if (gs.getProperty('notimecard_submission_notifyMe') == userNoTimeCard.user_name.toString() || gs.getProperty('notimecard_submission_notifyMe') == ''){
var noTimeCard = new GlideRecord("time_card");
noTimeCard.addEncodedQuery('user="+userNoTimeCard.sys_id+"^week_starts_on>=javascript:gs.daysAgoStart(8)^state!=Pending');
noTimeCard.query();
if (!noTimeCard.next()){
gs.log("AST:AST:TimeCard-TimeCardNotSubmittedNotification ---> User: " + userNoTimeCard.sys_id, "AST:AST:TimeCard-No Submission: User");
gs.eventQueue("notimecard.submit", userNoTimeCard, userNoTimeCard.sys_id.toString(), userNoTimeCard.getDisplayValue());
}
}
}
}
})();
It would get all the active users for the "Alphaserve" company and then check to see if the user has the "itil" role.
And it's always safer to use a Self-Executing Anonymous Function (or IIFE) style function block with your server-side code to protect it from the global scope as well as protecting the global scope from your code.