The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Adding a Conditional to a Scheduled Job

prudhvig
Tera Expert

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:

Screenshot (836).png

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

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:



find_real_file.png



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.


View solution in original post

9 REPLIES 9

Jim Coyne
Kilo Patron

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:



find_real_file.png



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.


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.


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.


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.

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.