Notification Email Script that loops through the values of the record

Khill
Tera Contributor

I'm curious if I'm able to do this. I want to create an notification email script. That retrieves the device(s) assigned to someone in my comp_device table. However, in my table a person can be assigned multiple devices. Each device being its own record with the assigned_to column being the link between all the records.  If they have one or more device(s) they are assigned I want to list them out. 

 

Example  - Notification email script 

var device = [];

email. setSubject('Your assigned Devices');

 

gr = new GlideRecord('comp_device');

gr.addQuery('assigned_to'. current.assigned_to);

gr.query();

 

while(gr.Next()){

device = gr.device_name;

}

var device_list = device.join('\n');

 

template.print('The list below are the device(s) assigned to you.');

template.print('Your Devices');

template.print(device_list);

 

Output: 

 

Hello "name",

 

The list below are the device(s) assigned to you.

 

Your Devices

Device 1

device 2

 

 

Am I thinking about this correctly? 

3 REPLIES 3

Berin
Tera Guru

Yes, that's exactly the approach I would use.  Are you having any problems with it?

Harish KM
Kilo Patron
Kilo Patron

HI @Khill Just one change bold line, you need to push array

var device = [];

email. setSubject('Your assigned Devices');

 

gr = new GlideRecord('comp_device');

gr.addQuery('assigned_to'. current.assigned_to);

gr.query();

 

while(gr.Next()){

device.push(gr.getValue('device_name')); //assuming not a reference field

}

var device_list = device.join('\n');

 

template.print('The list below are the device(s) assigned to you.');

template.print('Your Devices');

template.print(device_list);

Regards
Harish

SunilKumar_P
Giga Sage

Hi @Khill , The script provided by Harish should work, but there seems to be a type error on line 4, please check.

 

var device = [];

email. setSubject('Your assigned Devices');

gr = new GlideRecord('comp_device');

gr.addQuery('assigned_to'. current.assigned_to); // Change it to gr.addQuery('assigned_to', current.assigned_to);

gr.query();

while(gr.Next()){

device.push(gr.getValue('device_name')); //assuming not a reference field

}

var device_list = device.join('\n');

template.print('The list below are the device(s) assigned to you.');

template.print('Your Devices');

template.print(device_list);

 

Thanks,

Sunil