- 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 07:09 AM
Hi @ronro2,
Try the below script which differs from @Mark Manders script. Whilst this is not as efficient as Marks as he only requires one loop, I've made a few adjustments from a logical perspective and tested this on my PDI.
Clarification question - on line 2 of your script 'addEncodedQuery', you're trying to look for inactive managers (managed_by) and yet you want to also send notifications to these same managers? Note - ServiceNow by default will not send emails to inactive users.
I've made some tweaks and also provided some Best Practices like not using variables called gr. Test the below and un-comment the gs.print statements if you're testing via a Background script to confirm expected results.
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
var assetsByManager = 0;
var allAssetsInScope = 0;
var gaAssets = new GlideAggregate('alm_hardware');
gaAssets.addEncodedQuery('managed_by!=NULL^install_status=1'); //Adjust the conditions as you see fit but you cant send emails to inactive users so is your condition correct?
gaAssets.groupBy('managed_by')
gaAssets.query();
while (gaAssets.next()) {
var arr = [];
var grAssets = new GlideRecord('alm_hardware');
grAssets.addQuery('managed_by', gaAssets.managed_by);
grAssets.addQuery('install_status=1');
grAssets.query()
while (grAssets.next()) {
var details = grAssets.display_name + ' - ' + grAssets.managed_by.name;
arr.push(details + '');
allAssetsInScope++;
//gs.print('Testing details: ' + details);
}
gs.print('** FINAL **: ' + arr.toString());
assetsByManager++;
gs.eventQueue('vgr_inactive_hardware_manager', gaAssets, eventParms, gaAssets.managed_by);
}
gs.print('assetsByManager: ' + assetsByManager);
gs.print('allAssetsInScope: ' + allAssetsInScope);
- 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
- 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