Scripting Issue: SCTask Flow Email is not generating a complete description

SusanSchwar
Tera Contributor

I created a script for a multirow variable set. For each row in the request, it should populate in the description of the task and the system generated email should display the complete description of the task. However, say the user request two rows: Job 1 and Job2, Each with their respective fields. Job 1, the first row, will populate. But any other row added to the table will not generate in the body of the email. Please let me know what I need to change to make this work. 

(function execute(inputs){
var item_id = inputs.ritm;
//gs.log("MRVS item id " + item_id);
var ritmGR = new GlideRecord('sc_req_item');
if(ritmGR.get(item_id)){
    gs.log("MRVS get item " + ritmGR.number);
    var mrvs = ritmGR.variables.request_details;
    //gs.log("MRVS length " + mrvs.getRowCount());
} else {
    //gs.log("MRVS item not found");
}
var gr = new GlideRecord("sc_task");

    gr.initialize();
    gr.request = ritmGR.request;
    gr.request_item = ritmGR.sys_id;
    gr.priority = 4;
    // we need a glide record to lookup the assignment group and the state needs to be set
    var grGroup = new GlideRecord("sys_user_group");
    grGroup.addQuery("name", "*team name");
    grGroup.query();
    if(grGroup.next()){
        gr.assignment_group = grGroup.sys_id;
    }
    gr.state = 1;
    gr.short_description = "Run Job Name:"; 
    gr.description = "Run the Job names listed in the details below. \n";
    gr.description += "Run date: " + ritmGR.variables.run_start_date + "\n";
    gr.description += "Run time: " + ritmGR.variables.run_time + "\n";
    gr.short_description += " " + mrvs[0].job_group_name;
for(var row = 0; row < mrvs.getRowCount(); row++) {
    //gs.log("MRVS row " + row);
    if (row ==0){ 
    gr.description += "\nJob name: " + mrvs[row].job_group_name; 
    if(mrvs[row].input_file_path){
        gr.description += "\nInput file path: " + mrvs[row].input_file_path;
    } else {
        gr.description += "\nInput file path: none";
    }
    if(mrvs[row].predecessor){
        gr.description += "\nPredecessor: " + mrvs[row].predecessor;
    } else {
        gr.description += "\nPredecessor: none";
    }
    if(mrvs[row].successor){
        gr.description += "\nSuccessor: " + mrvs[row].successor;
    } else {
        gr.description += "\nSuccessor: none";
    }
     if(mrvs[row].parameters){
        gr.description += "\nParameters: " + mrvs[row].parameters;
    } else {
        gr.description += "\nParameters: none";
    }
    if(mrvs[row].special_instructions){
        gr.description += "\nSpecial instructions: " + mrvs[row].special_instructions + "\n\n";
    } else {
        gr.description += "\nSpecial instructions: none\n\n";
    }

}
}
gr.insert();
})(inputs);
4 REPLIES 4

Siddhesh Jadhav
Kilo Sage
Kilo Sage

Hi @SusanSchwar,


Root causes

  1. All of your row-processing code is inside if (row == 0) — that means only the first row ever gets appended.

  2. The multi-row variable object should be accessed via its row helper methods rather than mrvs[row] in some contexts. Use getRowCount() and getRow(i) which reliably return an object with the column names as properties.

Try this 

(function execute(inputs){
    var item_id = inputs.ritm;
    var ritmGR = new GlideRecord('sc_req_item');
    if(!ritmGR.get(item_id)){
        gs.log("MRVS item not found: " + item_id);
        return;
    }

    var mrvs = ritmGR.variables.request_details; // multi-row variable object

    var gr = new GlideRecord("sc_task");
    gr.initialize();
    gr.request = ritmGR.request;
    gr.request_item = ritmGR.sys_id;
    gr.priority = 4;

    // assignment group lookup
    var grGroup = new GlideRecord("sys_user_group");
    grGroup.addQuery("name", "*team name"); // adjust as needed
    grGroup.query();
    if(grGroup.next()){
        gr.assignment_group = grGroup.sys_id;
    }

    gr.state = 1;
    gr.short_description = "Run Job Name:";
    // build description
    gr.description = "Run the Job names listed in the details below.\n";
    gr.description += "Run date: " + ritmGR.variables.run_start_date + "\n";
    gr.description += "Run time: " + ritmGR.variables.run_time + "\n\n";

    // use getRowCount() and getRow(i)
    var rowCount = mrvs.getRowCount ? mrvs.getRowCount() : 0;
    if (rowCount > 0) {
        // put first job name in short_description if you want
        var firstRow = mrvs.getRow(0);
        if (firstRow && firstRow.job_group_name)
            gr.short_description += " " + firstRow.job_group_name;
    }

    for (var row = 0; row < rowCount; row++) {
        var r = mrvs.getRow(row); // object for this row
        if (!r) continue;

        gr.description += "Job name: " + (r.job_group_name || "none") + "\n";
        gr.description += "Input file path: " + (r.input_file_path || "none") + "\n";
        gr.description += "Predecessor: " + (r.predecessor || "none") + "\n";
        gr.description += "Successor: " + (r.successor || "none") + "\n";
        gr.description += "Parameters: " + (r.parameters || "none") + "\n";
        gr.description += "Special instructions: " + (r.special_instructions || "none") + "\n\n";
    }

    gr.insert();
})(inputs);

 

Thanks 

Siddhesh Jadhav

Servicenow Rising Star 2025

No, I'm afraid this didn't work. Now none of the jobs populate in the email. Only the short description, description, run date, and run time appear in the email. 

Ankur Bawiskar
Tera Patron
Tera Patron

@SusanSchwar 

not a good idea to create sc_task via script when you can easily create it using "Create Catalog Task" flow action.

try this once

(function execute(inputs) {
    var item_id = inputs.ritm;
    var ritmGR = new GlideRecord('sc_req_item');
    if (ritmGR.get(item_id)) {
        gs.log("MRVS get item " + ritmGR.number);
        var mrvs = ritmGR.variables.request_details;
    } else {
        gs.log("MRVS item not found");
        return;
    }

    var gr = new GlideRecord("sc_task");
    gr.initialize();
    gr.request = ritmGR.request;
    gr.request_item = ritmGR.sys_id;
    gr.priority = 4;

    var grGroup = new GlideRecord("sys_user_group");
    grGroup.addQuery("name", "*team name");
    grGroup.query();
    if (grGroup.next()) {
        gr.assignment_group = grGroup.sys_id;
    }
    gr.state = 1;

    gr.short_description = "Run Job Names: ";
    var jobNames = [];

    gr.description = "Run the Job names listed in the details below.\n";
    gr.description += "Run date: " + ritmGR.variables.run_start_date + "\n";
    gr.description += "Run time: " + ritmGR.variables.run_time + "\n";

    for (var row = 0; row < mrvs.getRowCount(); row++) {
        jobNames.push(mrvs[row].job_group_name);
        gr.description += "\nJob name: " + mrvs[row].job_group_name;
        gr.description += "\nInput file path: " + (mrvs[row].input_file_path || "none");
        gr.description += "\nPredecessor: " + (mrvs[row].predecessor || "none");
        gr.description += "\nSuccessor: " + (mrvs[row].successor || "none");
        gr.description += "\nParameters: " + (mrvs[row].parameters || "none");
        gr.description += "\nSpecial instructions: " + (mrvs[row].special_instructions || "none") + "\n\n";
    }

    gr.short_description += jobNames.join(", ");

    gr.insert();
})(inputs);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

SusanSchwar
Tera Contributor

No, I'm afraid this didn't work. Now none of the jobs populate in the email. Only the short description, description, run date, and run time appear in the email.