Email script & Notification

rahul122
Tera Contributor

My requirement is in the notification if i have 5 catalog items in my name all the catalog items name should be populated in the email body notication.

 

I have created the notification from the sys_user table and it is triggering by using event that is triggering emails  as expected but i m not able to populate catalog items name in my email notification.

I have created an email script to retrieve and list the catalog item names and included that script in the notification but it is not working 

 

Email script that i have created:

{

    var list = '';

    var gr = new GlideRecord('sc_cat_item');
    gr.addQuery('active', 'true');
    gr.addQuery('owner', current.sys_id);
    gr.query();
    while (gr.next()) {
        list += '-' + gr.getDisplayName('name') + '\n';
    }
    return list;

 

 

 

7 REPLIES 7

Chaitanya ILCR
Kilo Patron

Hi @rahul122 ,

 

update your notification email script as below

ChaitanyaILCR_0-1750933617668.png

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var list = '';
    var catGr = new GlideRecord('sc_cat_item');
    catGr.addActiveQuery();
    catGr.addQuery('owner', current.getValue('sys_id'));
    catGr.query();
    while (catGr.next()) {
        list += '-' + catGr.getDisplayName('name') + '\n';
    }
    template.print('<div>' + list + '</div>');


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

 and add this in the notification body

${mail_script:userCatalogNames} 

 

Note: I have give email script name as userCatalogNames r replace it with your email script name

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

Hi @Chaitanya ILCR  thank you for your reply ,

I have tried your script now it is returning only as 'name' , it is not showing name of the catalog item's. 

Hi @rahul122 ,

update as this

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var list = '';
    var catGr = new GlideRecord('sc_cat_item');
    catGr.addActiveQuery();
    catGr.addQuery('owner', current.getValue('sys_id'));
    catGr.query();
    while (catGr.next()) {
        template.print('<p> - ' + catGr.getDisplayValue() + '</p>');
    }


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

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

SumithraM
Tera Expert

Hi @rahul122

 

You can use array to achieve it, and print the Catalog names. Below code will have Catalog item names listed one by one. Be sure to call email script in notification with syntax - ${mail_script:<email_script_name>}

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {


    var itemNames = [];
    // Query the catalog items (sc_cat_item) where the current user is the owner
    var itemGR = new GlideRecord('sc_cat_item');
    itemGR.addQuery('owner', current.sys_id);
    itemGR.query();
    while (itemGR.next()) {
        itemNames.push(itemGR.getValue('name'));
    }
    for (i in itemNames) {
        template.print(itemNames[i] + '<br>');
    }
	

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

 

SumithraM_1-1750934657772.pngSumithraM_2-1750934663674.png