Trigger a email to the catalog item owner when job runs

vinuth v
Tera Expert

Hi All,

 

I need to trigger a email notification to the catalog item owner when the schedule job runs, for this I have created the event registry, Notification and email script. It's working as expected but facing one issue like body of the email Hi contains the static person(one specific name is displaying for all the email body) in all the email notification but I need to send the email to catalog item owner addressing like Hi {catalog item owner name need to display here}

 

 

Email body contains:

Hi ${u_cat_item_owner},

You have received this email because you are the owner of the catalog item ${mail_script:Catalog_item_owner_email_script}
Please review the form for any of the below points.

 

Schedule Job,

If the catalog owner has owning multiple catalog item then need to send single email the owner, this script is working fine

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();
if (!ownerItems[owner]) {
            ownerItems[owner] = [];
        }
ownerItems[owner].push(itemName);
    }
}
// Send notifications to owners with more than one item
for (var ownerKey in ownerItems) {
    if (ownerItems[ownerKey].length > 1) {
        var itemNames = ownerItems[ownerKey].join(', '); 
        gs.eventQueue('Catalog.item.owner.notification', gr, ownerKey, itemNames);
    }
}
 
Notification 
vinuthv_0-1694172080428.png

 

vinuthv_1-1694172109137.png

 

vinuthv_2-1694172224577.png

 

Here email will trigger to specific catalog item owner but 

Body of the email like Hi part contains the static person for all the email notifications

like below

vinuthv_3-1694172434553.png

 

 

Email Script:

vinuthv_4-1694172975441.png

 

 

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

 

6 REPLIES 6

msd93
Kilo Sage

Hi @vinuth v 

 

Did you check if the owner of all the catalog items are different and not the same person named  "Richard Kelley"

Hi @msd93 

 

All catalog item owners are different but for all notifications "Richard Kelly" is coming on the email body.

msd93
Kilo Sage

Hi @vinuth v 

Please try the below code in scheduled script:

(function() {
   
    
    // Define the query to retrieve catalog items and owners
    var catalogItemGr = new GlideRecord('sc_cat_item');
    catalogItemGr.addQuery('active=true^owner!=NULL'); // Filter for active catalog items
    catalogItemGr.query();
    
    // Map to store owners and their items
    var ownerItemsMap = {};
    
    while (catalogItemGr.next()) {
        var owner = catalogItemGr.owner.toString();
        var itemName = catalogItemGr.name.toString();
        
        // Add the item to the owner's list
        if (!ownerItemsMap[owner]) {
            ownerItemsMap[owner] = [];
        }
        ownerItemsMap[owner].push(itemName);
    }
    
    // Send emails to owners with their items
    for (var owner in ownerItemsMap) {
        var itemsList = ownerItemsMap[owner].join(',');
        gs.eventQueue('email.send', current, owner,  itemsList);
    }
})();
 
In email script use below:
(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

          // Add your code here
          var items = event.parm2;
          var owner = event.parm1;
          var users = new GlideRecord('sys_user');
          users.addQuery('sys_id',owner);
          users.query();
          if(users.next())
          {
          template.print('Hi '+users.name+ ',<br>');
          }
          template.print('You own catalog items: '+ items);

})(current, template, email, email_action, event);
 
Below is screenshot of email script:
msd93_0-1694700009450.png

Below is scheduled script:

msd93_1-1694700041062.png

Below is notification which triggers on event,

msd93_2-1694700071729.png

Below is the notification triggered.

msd93_3-1694700094665.png

Below are the notifications received, since i had only 3 catalog owners where one had 5 items and the other 1 item each.

System admin is owner of 5 catalog items, below email is sent

msd93_4-1694700162364.png

Abel is owner of 1 catalog item and below is email sent:

msd93_5-1694700225990.png

 

 

Make sure all the owners have email in their user profile. I hope this helps you.

Hi @msd93 ,

 

I tried same for my requirements it works ok but having some issues, 

 

There are 3 catalog item Test user owns, I checked logs and url is returning 3 different links BUT in notification only first one is coming correctly and for other 2, sys_id is blank. and Catalog item is are not coming in line even after using "\n"

 

Email Script:

 

RutujaK_1-1707948376631.png