Call script include from a schedule job and then trigger notification
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2023 11:59 PM
Hi All,
I have a requirement, where i have created a script include and i need to call this script include in a schedule job which runs periodically and when script include returns true i need to trigger notification i.e call a event in schedule job.
Below is the Script include
Script include name - CriteriaforUser
Test1 : function(usersysid){
var Date1 ='';
var encqry='some query';
var grUserRec = new GlideRecord('sys_user');
grUserRec.addQuery('sys_id',usersysid);
grUserRec.addEncodedQuery(encqry);
grUserRec.query();
if(grUserRec.next()){
Date1 = grUserRec.date;
var gdt2 = new GlideDateTime();
var gdt = new GlideDateTime(Date1);
var strtdate = gdt.getDate();
var dgt = new GlideDateTime();
var nowdt = dgt.getDate();
var duration = gs.dateDiff(nowdt, strtdate, true);
if(duration>0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
},
I need to call this script in a schedule job and need to trigger the event once the script include returns true.
Can someone help me with the schedule job as i am not understaing the way to call it.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2023 03:13 AM
based on which user you are checking the logic?
in script include function it required user sysId. whose sysId is that?
Notification is on which table
what's the exact business requirement here?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2023 11:10 PM
Hi @Ankur Bawiskar ,
This logic will be triggered by a schedule job which runs twice a day, and the schedule job calls the script include, which contains the logic i.e users who belong to particular set of companies/department will be gathered and once the script include returns true, we will trigger notification from the schedule job.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2023 11:32 PM
then why are you querying with user sysId
you already have query
also where is the script to trigger event? you should be using gs.eventQueue()
Is the notification etc setup done?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 11:40 PM
Below is the schedule job that i have created.
var getUser = new GlideRecord("sys_user");
getUser.addQuery('active', true);
getUser.query();
while(getUser.next()){
if(new CriteriaforUser().Test1(getUser.sys_id) == true){
gs.eventQueue();
}
}
and this is the script include
Script include name - CriteriaforUser
Test1 : function(usersysid){
var Date1 ='';
var encqry='some query';
var grUserRec = new GlideRecord('sys_user');
grUserRec.addQuery('sys_id',usersysid);
grUserRec.addEncodedQuery(encqry);
grUserRec.query();
if(grUserRec.next()){
Date1 = grUserRec.date;
var gdt2 = new GlideDateTime();
var gdt = new GlideDateTime(Date1);
var strtdate = gdt.getDate();
var dgt = new GlideDateTime();
var nowdt = dgt.getDate();
var duration = gs.dateDiff(nowdt, strtdate, true);
if(duration>0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 11:46 PM
why to query again in script include when you are already doing it in schedule job.
Move the entire code to script include and update schedule job and script include as this
Script include:
function Test1(){
var Date1 ='';
var encqry='some query';
var grUserRec = new GlideRecord('sys_user');
grUserRec.addActiveQuery();
grUserRec.addEncodedQuery(encqry);
grUserRec.query();
if(grUserRec.next()){
Date1 = grUserRec.date;
var gdt2 = new GlideDateTime();
var gdt = new GlideDateTime(Date1);
var strtdate = gdt.getDate();
var dgt = new GlideDateTime();
var nowdt = dgt.getDate();
var duration = gs.dateDiff(nowdt, strtdate, true);
if(duration>0)
{
gs.eventQueue('event_name', grUserRec, grUserRec.getUniqueValue());
}
}
},
Scheduled job
new CriteriaforUser().Test1();
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader