- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-17-2016 02:14 AM
Hi ,
I would like to trigger an event from scheduled job based on count if greater than 0.
The event triggers but the state always showing as error.
Here is my script.
var dt=gs.nowDateTime();
var lutil = new Missing_computers().getCount();
//gs.print(lutil);
var gr=new GlideRecord('u_ashleyfurniture_domain_computers');
gr.addEncodedQuery('u_cnIN' + lutil);
//gr.addEncodedQuery('operational_statusIN1,3,4');
gr.query();
var count=gr.getRowCount();
if(count>0){
gs.eventQueue('missing.ad.records.cmdb', gr, gs.getUserID());
}
while(gr.next())
{
var gr1=new GlideRecord('u_missing_cmdb_computers');
gr1.initialize();
//gr1.setWorkflow(false);
gr1.u_created_data=dt;
gr1.u_source=gr.u_source;
gr1.u_cn=gr.u_cn;
gr1.insert();
}
Thanks,
Jagarnath
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-17-2016 03:04 AM
I realized later,The reason why the job is not getting processed is, it is not having any glide record queried to get the output result. I just applied some logic and the notification is triggering now.
Note: I have replaced my table with the sys_user and if i need to get any info from the other tables in the notification content, i will take the help of notification email script.
Here is my revised code.
var dt=gs.nowDateTime();
var lutil = new Missing_computers().getCount();
//gs.print(lutil);
var gr=new GlideRecord('u_ashleyfurniture_domain_computers');
gr.addEncodedQuery('u_cnIN' + lutil);
//gr.addEncodedQuery('operational_statusIN1,3,4');
gr.query();
var count=gr.getRowCount();
if(count>0){
var gr2= new GlideRecord('sys_user');
gr2.addQuery('sys_id', 'c1ec13d80f0a650000af0bcce1050ea0');
gr2.query();
if(gr2.next()){
gs.eventQueue('missing.ad.records.cmdb', gr2, gr2.sys_id, count);
}
}
while(gr.next())
{
var gr1=new GlideRecord('u_missing_cmdb_computers');
gr1.initialize();
//gr1.setWorkflow(false);
gr1.u_created_data=dt;
gr1.u_source=gr.u_source;
gr1.u_cn=gr.u_cn;
gr1.insert();
}
Thanks,
Jagarnath
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-17-2016 02:54 AM
Good catch, sorry I copied the original row and just took away the value inside,
I havent caught the quote
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-17-2016 03:15 AM
Glad if that works out.
To add,
Avoid using hard-coded values in scripts, as they can lead to unpredictable results and can be difficult to track down later. Hard coding sys_ids is not recommended, as they may not be the same between instances. Instead, try looking up a value by reference or by creating a property and retrieving the value with gs.getProperty().
Coding Best Practices - ServiceNow Wiki