How to separate the items one by one using bullet point or number using script

vinuth v
Tera Expert

Hi All,

 

I have written the schedule job for trigger the email notification to the catalog item owner.

 

var gr = new GlideRecord('sc_cat_item');
gr.addQuery('active=true^u_cat_item_owner.active=true^u_cat_item_ownerISNOTEMPTY');
//gr.addQuery('active=true^u_cat_item_owner.active=true^u_cat_item_ownerISNOTEMPTY^nameSTARTSWITHRetire a Standard Change Template');
gr.query();
 
// Create an object to store item names grouped by owner
var ownerItems = {};
 
while (gr.next()) {
    var owner = gr.u_cat_item_owner;
//gs.log("ownerowner"+owner);
    // Check if the owner is defined
    if (owner) {
        var itemName = gr.name.toString();
 
        // Initialize an array for the owner if it doesn't exist
        if (!ownerItems[owner]) {
//gs.log("ownerItems56"+ownerItems[owner]);
            ownerItems[owner] = [];
        }
 
        // Push the item name into the owner's array
ownerItems[owner].push(itemName);
//gs.log("ownerItems62"+ownerItems[owner]);
    }
}
 
// Send notifications to owners with more than one item
for (var ownerKey in ownerItems) {
//gs.log("ownerItems[ownerKey].length" + ownerItems[ownerKey].length);
    if (ownerItems[ownerKey].length >= 1) {
        var itemNames = ownerItems[ownerKey].join(', '); // Join item names into a comma-separated string
//gs.log("itemNames"+itemNames);
//gs.log("ownerKey"+ownerKey);
        gs.eventQueue('Catalog.item.owner.notification', gr, ownerKey, itemNames);
}
}

 

The above script is working as expected, currently I am separating catalog item by " , " instead of comma I wanted to separate the catalog item one by one like using bullet or number.
 
vinuthv_1-1695207744007.png

 

 
Please any one provide me the input,
 
Thanks in advance,
Vinuth
 
6 REPLIES 6

@vinuth v 

then do this

ownerItems[owner].push(itemName.toString());
 
var itemNames = ownerItems[ownerKey].toString();
 
Email script:

var values = event.parm2;

var arr = values.split(',');

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

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Tushar
Kilo Sage
Kilo Sage

Hi @vinuth v 

 

Please try below - 

 

var gr = new GlideRecord('sc_cat_item');
gr.addQuery('active=true^u_cat_item_owner.active=true^u_cat_item_ownerISNOTEMPTY');
gr.query();

// Create an object to store item names grouped by owner
var ownerItems = {};

while (gr.next()) {
    var owner = gr.u_cat_item_owner;

    // Check if the owner is defined
    if (owner) {
        var itemName = gr.name.toString();

        // Initialize an array for the owner if it doesn't exist
        if (!ownerItems[owner]) {
            ownerItems[owner] = [];
        }

        // Push the item name into the owner's array with <li> tags
        ownerItems[owner].push('<li>' + itemName + '</li>');
    }
}

// Send notifications to owners with more than one item
for (var ownerKey in ownerItems) {
    if (ownerItems[ownerKey].length >= 1) {
        // Wrap the joined items in <ul> and </ul> tags to create a bulleted list
        var itemNames = '<ul>' + ownerItems[ownerKey].join('') + '</ul>';
        gs.eventQueue('Catalog.item.owner.notification', gr, ownerKey, itemNames);
    }
}

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar