Error creating a notification.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello everyone,
Currently in our instance there are some items of multi SC_Task type, which I have seen are evaluated at the notification level through this code:
var reqItemSysId = current.request_item.sys_id;
var variables = {};
var itemOptions = new GlideRecord('sc_item_option_mtom');
itemOptions.addQuery('request_item', reqItemSysId);
itemOptions.query();
while (itemOptions.next()) {
variables[itemOptions.sc_item_option.item_option_new.name] = itemOptions.sc_item_option.value;
}
if (variables['multitask'] == 'true'){
answer = true;
}else{
answer = false;
}
The fact is that I have to create a notification that is sent when an "additional comment" is added to the parent RITM and it is sent to all the assigned groups (which are usually several and different from those of the parent RITM) to each of the "child SC_Tasks". For this I have created the notification, a BR and an event that I leave here, but I can't get it to work, any ideas.
Notification:
Event:
BR:
(function executeRule(current, previous) {
if (current.additional_comments == previous.additional_comments)
return;
var vars = {};
var opt = new GlideRecord('sc_item_option_mtom');
opt.addQuery('request_item', current.sys_id);
opt.query();
while (opt.next()) {
vars[opt.sc_item_option.item_option_new.name] = opt[opt.sc_item_option.value];
}
if (String(vars['multitask']).toLowerCase() !== 'true')
return;
var child = new GlideRecord('sc_task');
child.addQuery('request_item', current.sys_id);
child.query();
while (child.next()) {
if (child.assignment_group) {
gs.eventQueue(
'atc.ritm.multitask.comment_added',
child,
current.additional_comments,
''
);
}
}
})(current, previous);
Thank you very much in advance and greetings.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
At a quick glance I would say it is that your event registration has table as sc_req_item but your notification is on sc_task and you are creating the events with sc_tasks.
Also consider how you access the variable values of your item not just for performance but for also readability. When you dotwalk inside a gliderecord loop you end up doing wasteful round trips to the database when you can just let the database handle the work.
var gr = new GlideRecord("sc_item_option_mtom");
gr.addQuery("request_item", reqItemSysId)
.addCondition("sc_item_option.item_option_new.name", "multitask")
.addCondition("sc_item_option.value", "true")
gr.setLimit(1);
gr.query();
answer = gr.hasNext();
//when current is sc_req_item
current.variables.multitask.getValue() === "true"Also there is no point in passing record fields of the event record as event parameters as everywhere that you handle events the platform will query the record for you anyway.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi, I've modified what you suggested and now if I force the comment with a background script it works, but not if I do it manually.
Background Script:
gs.info("🧪 ===== FORZANDO BR MULTITASK DESDE WORKSPACE =====");
var ritmSysId = "b4630b508725ba14b44aeb909bbb352e";
var ritmGR = new GlideRecord("sc_req_item");
if (ritmGR.get(ritmSysId)) {
gs.info("✅ RITM encontrado: " + ritmGR.number);
} else {
gs.info("❌ RITM no encontrado: " + ritmSysId);
}
var journal = new GlideRecord("sys_journal_field");
journal.addQuery("element_id", ritmSysId);
journal.orderByDesc("sys_created_on");
journal.setLimit(1);
journal.query();
var comentario = "";
if (journal.next()) {
comentario = journal.getValue("value") || "";
gs.info("💬 Último comentario del Journal raíz: " + comentario);
} else {
gs.info("⚠ No hay comentarios en Journal para: " + ritmSysId);
}
var isMultitask = false;
var m2m = new GlideRecord("sc_item_option_mtom");
m2m.addQuery("request_item", ritmSysId);
m2m.addQuery("sc_item_option.item_option_new.name", "multitask");
m2m.query();
if (m2m.next()) {
if (String(m2m.sc_item_option.value).toLowerCase() == "true") {
isMultitask = true;
}
}
gs.info("📌 ¿Es multitask según MTOM? → " + isMultitask);
if (isMultitask && comentario) {
var child = new GlideRecord("sc_task");
child.addQuery("request_item", ritmSysId);
child.query();
while (child.next()) {
gs.eventQueue(
"atc.ritm.multitask.comment_added",
child,
comentario.trim(),
ritmSysId
);
gs.info("🔔 Evento reenviado a hermana: " + child.number + " | Grupo: " + (child.getValue("assignment_group") || "sin grupo"));
}
} else {
gs.info("⛔ No reenviamos evento (o no es multitask o journal vacío).");
}
gs.info("🏁 ===== FIN TEST =====");BR:
(function executeRule(current, previous) {
if (current.additional_comments.nil()) return;
var comentario = current.additional_comments.getJournalEntry(1);
if (!comentario) return;
var rootTask = new GlideRecord("sc_task");
rootTask.addQuery("request_item", current.sys_id);
rootTask.orderByDesc("sys_created_on");
rootTask.setLimit(1);
rootTask.query();
if (!rootTask.next()) return;
var isMultitask = false;
var m2m = new GlideRecord("sc_item_option_mtom");
m2m.addQuery("request_item", current.sys_id);
m2m.addQuery("sc_item_option.item_option_new.name", "multitask");
m2m.query();
if (m2m.next()) {
if (String(m2m.sc_item_option.value).toLowerCase() == "true") {
isMultitask = true;
}
}
if (!isMultitask) return;
var child = new GlideRecord("sc_task");
child.addQuery("request_item", current.sys_id);
child.query();
while (child.next()) {
gs.eventQueue(
"atc.ritm.multitask.comment_added",
child,
comentario.trim(),
current.sys_id
);
}
})(current, previous);
Event:
Notification:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Additional comments field name is just comments not additional_comments and I would also check the nil() behaviour for journal fields so that it actually matches what one would expect.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi Lauri, thanks for your reply.
Then I'll modify what you suggested and check the behavior.
Thanks a lot.