Email Notification to all users of assets assigned specifically to them

Moedeb
Tera Guru

I am wanting to do something very similar to the solution listed in the following article

https://community.servicenow.com/community?id=community_question&sys_id=f4f4c72ddbd8dbc01dcaf3231f96...

What I want to do is send an email to all users that lists all assets that are assigned to them.

I'm guessing I could trigger a email notification via a business rule or an event (this is something that could be a one off or at least used only very occasionally)

I want it to go through each user, add their assets, send the email, move onto the next user, once all have been emailed stop!

The link above does not work for me as it pretty much generates an email with every single asset listed on it, nothing specific to an individual user.

Does anyone know how I can do this?

 

1 ACCEPTED SOLUTION

@Moedeb 

It should look like this in script

(function runMailScript(current, template, email, email_action, event) {

var user = event.parm1;

var arr = [];

var gr = new GlideRecord('cmdb_ci'); // table name changed
gr.addQuery('assigned_to', user);
gr.query();
while(gr.next()){

var str = gr.asset_tag + ' - ' + gr.name + ' - ' + gr.model_id.getDisplayValue();

arr.push(str.toString());

}

template.print('<ul>');
for(var i=0;i<arr.length;i++){
template.print('<li>' + arr[i] + '</li>')
}
template.print('<ul>');

})(current, template, email, email_action, event);

I hope this will help and answer will be marked as correct and helpful as most of the information is already shared.

Have nice day

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

19 REPLIES 19

Thank you.

Happy learning

Regards

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

where job script need to write either schedule jobs or script include 

Hi,

I have provided sample script to your question

https://community.servicenow.com/community?id=community_question&sys_id=47f2bdf6db6ad09466f1d9d96896...

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

asifnoor
Kilo Patron

Hi

1. Create a event on asset table

2. Write a script (fix script) and get all unique assigned users of assets and trigger event for each assigned user

var gr = new GlideRecord("alm_asset");
gr.addEncodedQuery("assigned_toISNOTEMPTY");
gr.query();
var users = [];
while(gr.next()) {
  users.push(gr.assigned_to.toString());
}
var arrayUtil = new ArrayUtil();
users = arrayUtil.unique(users);
//Here now either you can trigger 1 event and send all users as 1 string and parse them inside mail script. But if the string is bigger, it might break
//so triggering event per user as its easier to track
for(i=0;i<users.length;i++) {
  gs.print(users[i]);
  gs.eventQueue("your_event_name",current,users[i],gs.getUserName());
}

3. Create a notification which listen to yoru event

4. Then inside notification, add mail script and add below code.

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
  var gr = new GlideRecord('alm_asset');
  gr.addEncodedQuery('assigned_to', event.parm1);
  gr.query();
  while(gr.next()){
    template.print(gr.name+'<br/>');
  }
})(current, template, email, email_action, event);

 

 

Mark the comment as a correct answer and also helpful if this answers your question.

Hi,

If this has answered your question, mark the comment as a correct answer and helpful.