- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 03:51 AM
Hi All,
I have a requirement where I need to send an email to specific email id which will include all the assets name added in cmdb_ci table today in one notification. And email should trigger only when something is added. So far I did:
created scheduled job which will check for today's record and event will be triggered. Also created email script.
Scheduled job:
var row;
var nm =[];
var length;
var mailId = 'x@abc.com';
var gr = new GlideRecord('cmdb_ci');
gr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
gr.query();
row = gr.getRowCount();
gs.log("rowcount "+row);
while(gr.next()){
nm.push(gr.getValue('name'));
}
length = nm.length;
gs.log("Name"+nm);
gs.log("Length "+length);
if(row > 0){
for (var x=0; x<length; x++){
gs.eventQueue('Insertion in CMDB table',gr,nm[x],mailId);
}
}
Email Script:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
// Add your code here
var row;
var x;
template.print('Hi All,\n\n');
template.print('<br><br>Below entries are added today in CMDB table');
template.print('<table border="1"><th colspan="4" style="text-align:center">CMDB Entries</th>');
template.print("<tr><th>Name of Assest</th></tr>");
var nm1 = [];
nm1.push(event.parm1);
var l = nm1.length;
gs.log('length/11 '+l);
gs.log('name_email_script' + nm1);
var cmdb = new GlideRecord('cmdb_ci');
cmdb.addEncodedQuery('name=' +nm1+ '^sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
cmdb.query();
var row = cmdb.getRowCount();
gs.log("Count_email_script "+cmdb.getRowCount());
while(cmdb.next()){
gs.log('inside while loop');
if(row > 0){
var link = ("<tr><th>"+cmdb.getValue('name')+"</th></tr>");
template.print(link);
}}
})(current, template, email, email_action, event);
But it is sending separate email for separate entry in cmdb table. I tried removing the loop from scheduled job which is adding before event call but this way email script does not run properly as names are coming into array all together.
Need expert advice on this.
Thanks in advance,
Shivani
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 04:34 AM
Hello @Shivani29 ,
Please give a try to the modified scripts below and let me know how it works for you.
Scheduled Job:
var nm = [];
var mailId = 'x@abc.com';
var gr = new GlideRecord('cmdb_ci');
gr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
gr.query();
while (gr.next()) {
nm.push(gr.getValue('name'));
}
if (nm.length > 0) {
gs.eventQueue('Insertion in CMDB table', gr, nm.join(','), mailId);
}
Email Script:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
// Add your code here
var nm1 = event.parm1.split(',');
template.print('Hi All,\n\n');
template.print('<br><br>Below entries are added today in CMDB table');
template.print('<table border="1"><th colspan="4" style="text-align:center">CMDB Entries');
template.print("<tr><th>Name of Asset</th></tr>");
for (var i = 0; i < nm1.length; i++) {
template.print("<tr><th>" + nm1[i] + "</th></tr>");
}
})(current, template, email, email_action, event);
Let me know your views on this and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 04:34 AM
Hello @Shivani29 ,
Please give a try to the modified scripts below and let me know how it works for you.
Scheduled Job:
var nm = [];
var mailId = 'x@abc.com';
var gr = new GlideRecord('cmdb_ci');
gr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
gr.query();
while (gr.next()) {
nm.push(gr.getValue('name'));
}
if (nm.length > 0) {
gs.eventQueue('Insertion in CMDB table', gr, nm.join(','), mailId);
}
Email Script:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
// Add your code here
var nm1 = event.parm1.split(',');
template.print('Hi All,\n\n');
template.print('<br><br>Below entries are added today in CMDB table');
template.print('<table border="1"><th colspan="4" style="text-align:center">CMDB Entries');
template.print("<tr><th>Name of Asset</th></tr>");
for (var i = 0; i < nm1.length; i++) {
template.print("<tr><th>" + nm1[i] + "</th></tr>");
}
})(current, template, email, email_action, event);
Let me know your views on this and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 09:58 PM
Thanks Aniket, It is working but now I need to add 2 more values in addition to name. so I modified the email script a bit but it is not giving any value now. Could you please check the script once.
var nm1 = event.parm1.split(',');
gs.log('nm1 length '+nm1.length);
template.print('Hi All,\n\n');
template.print('<br><br>Below entries are added today in CMDB table');
template.print('<table border="1"><th colspan="4" style="text-align:center">CMDB Entries');
template.print("<tr><th>Name of Assest</th><th>IP Address</th><th>Host Name</th></tr>");
var gr = new GlideRecord('cmdb_ci');
gr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^nameLIKE'+nm1);
gr.query();
while(gr.next()){
var count = gr.getRowCount();
if(count>0){
for (var i = 0; i < nm1.length; i++) {
template.print("<tr><th>" + nm1[i] + "</th><th>" + gr.getValue('ip_address') +"</th><th>" + gr.getValue('u_host_name') +"</th></tr>");
}}}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 11:38 PM
Hello @Shivani29 ,
Okay, then please try with the below script once which I have modified.
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
// Add your code here
var nm1 = event.parm1.split(',');
gs.log('nm1 length ' + nm1.length);
template.print('Hi All,\n\n');
template.print('<br><br>Below entries are added today in CMDB table');
template.print('<table border="1"><th colspan="4" style="text-align:center">CMDB Entries');
template.print("<tr><th>Name of Asset</th><th>IP Address</th><th>Host Name</th></tr>");
for (var i = 0; i < nm1.length; i++) {
var gr = new GlideRecord('cmdb_ci');
gr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^name=' + nm1[i]);
gr.query();
if (gr.next()) {
template.print("<tr><th>" + nm1[i] + "</th><th>" + gr.getValue('ip_address') + "</th><th>" + gr.getValue('u_host_name') + "</th></tr>");
}
}
template.print('</table>');
})(current, template, email, email_action, event);
Let me know your views on this and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 01:56 AM
Thanks Aniket. All working fine now. Just one change I made is captured sys ids in event parm1 and in email script I got the records on basis of sys id as I found that in some of the cases name is empty so host name getting undefined.
Regards,
Shivani
Regards,
Shivani