- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2024 05:14 AM - edited 08-13-2024 05:55 AM
Hello guys!
I want to trigger notifications via a Scheduled Job that's going to run once every month. It's based on alm_hardware table.
So there managed_by can have several posts of hardware assets under him/her. I want to trigger so that only one mail is sent to the "manager" of the "managed_by" (so managed_by.manager) containing all the assets beloning to the managed_by.
Here is my current code:
var gr = new GlideRecord('alm_hardware');
gr.addEncodedQuery('managed_by.active=false^install_status=1');
gr.query();
while (gr.next()) {
var manager = gr.managed_by.manager;
if (manager && manager.active) {
var eventParms = gr.display_name + ';' + gr.managed_by.name;
gs.eventQueue('vgr_inactive_hardware_manager', gr, eventParms, manager.sys_id);
}
}
This code doesn't do what it's intended to do, so it tons of mails, every time there is an asset post under a managed_by.
I want my notification to look like this:
Thanks in advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2024 05:45 AM
You are triggering an event per asset, so that explains the amount of emails.
var gr = new GlideRecord('alm_hardware');
gr.addEncodedQuery('managed_by.active=false^install_status=1');
gr.query();
var assetsByManager = {};
// Group assets by manager's sys_id
while (gr.next()) {
var manager = gr.managed_by.manager;
if (manager && manager.active) {
var managerSysId = manager.sys_id;
if (!assetsByManager[managerSysId]) {
assetsByManager[managerSysId] = {
managerName: manager.name,
assets: []
};
}
assetsByManager[managerSysId].assets.push(gr.display_name);
}
}
// Trigger event for each manager with the list of assets
for (var managerSysId in assetsByManager) {
var managerData = assetsByManager[managerSysId];
var assetList = managerData.assets.join(', '); // Concatenate assets into a single string
var eventParms = assetList + ';' + managerData.managerName;
gs.eventQueue('vgr_inactive_hardware_manager', null, eventParms, managerSysId);
}Can you check if this code will have better results?
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2024 07:46 AM
Thanks for an amazing reply, but I need to do a 180 https://www.servicenow.com/community/developer-forum/notification-triggerent-by-event-and-a-schedule...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2024 07:57 AM
Hi @ronro2,
So the script I provided does exactly this: Trigger one event (and subsequent notification) per "manager" containing all the Assets by the "managed_by" field.
Example: If there are 100 assets managed equally between 5 managers - 5 emails will be sent to each manager containing a list of the 20 assets assigned to them
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2024 03:39 AM
Hi @ronro2 ,
If Robbie's script in your new question helped you resolve it, can you accept his response here as solution as well, so this question gets closed? If someone reeds your question and misses your 180, they are spending time on something that is no longer required.
Thanks!
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark