How to update SCTASK worknotes when worknotes is added to RITM

sanasayyad
Tera Contributor

I have written a business rule on sc_req_item i.e after insert here is the code 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here

    current.work_notes = "test";
    current.update();

    //Now i want same thing on sc_task table so i have written this

    var id = current.sys_id;

    var grp = new GlideRecord("sc_task");
    grp.addQuery('request_item', id);
    gs.info("The ID is " + id);
    grp.query();

    gs.info("testing query: Row count = " + grp.getRowCount());

    if (grp.next()) {
        gs.info("test");
        grp.work_notes = "test ";
        grp.update();
    }
    else
    {
        gs.log("No sc_task records found for request item: " + id);
    }


})(current, previous);

In this i am not getting record by doing  grp.getRowCount() in system log i am getting 0.
2 REPLIES 2

Rajesh Chopade1
Mega Sage

hi @sanasayyad 

 

Instead of running the business rule on insert, configure it to run on update when the work_notes field changes. This ensures that sc_task records are more likely to exist, as they are typically created shortly after the RITM is inserted.

 

When a Requested Item (RITM) is created, the associated catalog tasks (sc_task) are usually generated by a workflow or script that runs after the RITM is inserted. At the moment your business rule executes (after insert on sc_req_item), the sc_task records may not yet exist in the database.

 

update your code as bellow and try once:

// Check if work_notes has been updated
    if (current.work_notes.changes()) {
        // Log the work notes update for debugging
        gs.info("Work notes updated on RITM: " + current.number + ", New work notes: " + current.work_notes);

        // Query related sc_task records
        var grp = new GlideRecord('sc_task');
        grp.addQuery('request_item', current.sys_id);
        grp.query();

        gs.info("Found " + grp.getRowCount() + " sc_task records for RITM: " + current.number);

        // Loop through all related tasks and update work_notes
        while (grp.next()) {
            grp.work_notes = current.work_notes; // Copy the RITM work notes to the task
            grp.setWorkflow(false); // Prevent triggering other business rules/workflows
            grp.update();
            gs.info("Updated work_notes for sc_task: " + grp.number);
        }

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh

 

Nilesh Pol
Tera Guru

@sanasayyad If your goal is to update work notes for every task created under a RITM, just move the logic to the sc_task table itself, When: After Insert, Condition: request_item != '' 

script:

(function executeRule(current, previous /*null when async*/ ) {

current.work_notes = "test from task";
current.update();

})(current, previous);