Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Error sending notification when adding a comment in SOW between tables RITM and SC_TASK

tomaslindev
Mega Guru

Hello everyone,
I want to send a notification to each of the child SC_Tasks of a multitask RITM when a comment is added to that RITM. Each SC_Task usually belongs to a different group. It works when the background script runs, but it doesn't when I add it manually. Any ideas?

 

Background Script:

gs.info("🧪 ===== FORCING BR MULTITASK FROM WORKSPACE =====");

// sys_id of the multitask RITM we have been using
var ritmSysId = "b4630b508725ba14b44aeb909bbb352e";

// 1. Load the parent RITM record
var ritmGR = new GlideRecord("sc_req_item");
if (ritmGR.get(ritmSysId)) {
    gs.info("✅ RITM found: " + ritmGR.number);
} else {
    gs.info("❌ RITM not found: " + ritmSysId);
}

// 2. Get the latest journal comment from sys_journal_field
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("💬 Latest comment from parent journal: " + comentario);
} else {
    gs.info("⚠ No journal comments found for: " + ritmSysId);
}

// 3. Check if the RITM is multitask by reading MTOM (exactly like your notification does)
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("📌 Is multitask according to MTOM? → " + isMultitask);

// 4. If multitask and comment exists, propagate the event to sibling SC Tasks
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("🔔 Event re-enqueued to sibling: " + child.number + " | Group: " + (child.getValue("assignment_group") || "no group"));
    }
} else {
    gs.info("⛔ Event not propagated (either not multitask or empty journal).");
}

gs.info("🏁 ===== END TEST =====");

 

BR:

 

tomaslindev_0-1764550929784.png

(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:

tomaslindev_1-1764550973973.png

 

Notification:

tomaslindev_2-1764551006242.png

 

tomaslindev_3-1764551026527.png

 

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

The name of the field on the sc_req_item table is 'comments' not 'additional_comments', so the script is not running, which you would see by adding logs, like you did in the background script.  Your non-standard GlideRecord format/approach on sc_task is unnecessary and probably isn't returning the record.

(function executeRule(current, previous) {
    if (current.comments.nil()) {
        return;
    }

    var comentario = current.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()) {
        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 (m2m.sc_item_option.value.toString().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);