Workflow not attached to custom/script generated RITM

Ruchi Kumari1
Tera Expert

Hi,
I am working a catalog, which generates multiple RITM based on users selected on form.
Each user selected should be updated on requested for field on RITM.
I am able to generate multiple RITMs(Default + script generated). but the script generated ritms are not having any workflow attached. There is no "show workflow" related list attached to it. Ideally script generated ritms should have that attached as well. 
I tried creating ritm from both runscript and create task activity but the result is same. Both of them have stage = request approved. Am i missing any other required configuration ?

RuchiKumari1_0-1752239447556.png

Runscript to create ritm :

 
var list = current.variables.requested_for.toString();
var array = list.split(','); //list of requested for users
for (var i = 1; i < array.length; i++) {
var grRitm = new GlideRecord("sc_req_item");
    grRitm.initialize();
    grRitm.cat_item = current.cat_item;
    grRitm.price = current.price;
    grRitm.request = current.request;
    grRitm.requested_for = array[i];
    grRitm.due_date = current.due_date;
    grRitm.short_description = 'test';
    grRitm.assignment_group = current.assignment_group;

    var ritmSysId = grRitm.insert();
}


Script in create task activity (for sc_req_item table):

var list = current.variables.requested_for.toString();
var array = list.split(',');
for (var i = 1; i < array.length; i++) {
    task.request = current.request;
    task.requested_for = array[i];
    task.cat_item = current.cat_item;
}

None of the approach is attaching workflow to the created ritm record. Default RITM is having workflow attached as expected.

8 REPLIES 8

Hi Ankur,
I am still checking if we can achieve this without cart api. As I've found some resources supporting this, I believe we can attach workflow through startflow(). I am trying them currently in my script. 
I will definitely close this thread and others once I reach a conclusion.

Ankur Bawiskar
Tera Patron
Tera Patron

@Ruchi Kumari1 

Would you mind closing your earlier questions by marking appropriate response as correct?

Members have invested their time and efforts in helping you.

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

JessicaLanR
Kilo Guru

Understood. Here's the revised response in English, without emojis:

The issue you're encountering—RITMs created via script without an attached workflow—occurs because using GlideRecord.insert() bypasses the standard mechanisms that normally trigger workflows in ServiceNow.

When a catalog item is requested through the standard Service Catalog UI, workflows are automatically triggered by system components like Business Rules, Catalog Flows, or Script Includes. Scripted inserts don’t go through these layers, which is why the workflow is not attached.

To fix this, you need to manually trigger the workflow after inserting the RITM. Here’s how you can do it:

 

var wf = new Workflow();
wf.startFlow('your_workflow_name_here', grRitm, 'insert');

 

If you're using the workflow defined on the Catalog Item, you can dynamically retrieve it like this:

 

var item = new GlideRecord('sc_cat_item');
if (item.get(current.cat_item)) {
  var wf = new Workflow();
  wf.startFlow(item.workflow, grRitm, 'insert');
}

 

Alternatively, you can use Flow Designer to manage the RITM generation and let the platform handle workflow association automatically, which is the recommended approach when possible.

After implementing this, test to confirm that the new RITMs have the expected workflow attached. If it works, consider marking this issue as resolved for tracking purposes.

Hi @JessicaLanR , I did try exectuing workflow from script but it's executing infinitely.
Here's what I tried:

var list = current.variables.requested_for.toString();
var array = list.split(','); //list of requested for users

for (var i = 1; i < array.length; i++) {
var grRitm = new GlideRecord("sc_req_item");
    grRitm.initialize();
    grRitm.cat_item = catalogItem; //cat item sys id
    grRitm.request = current.request;
    grRitm.requested_for = array[i];
    grRitm.short_description = "Lifecycle request";
    grRitm.assignment_group = current.assignment_group;
     var ritmSysId = grRitm.insert();


var itemID = ritmSysId;
var ritmVar = new GlideRecord('sc_item_option_mtom');
ritmVar.addQuery('request_item', current.getUniqueValue());
ritmVar.orderBy('sc_item_option.order');
ritmVar.query();
while (ritmVar.next()) {
        var grMtom = new GlideRecord('sc_item_option_mtom');
        grMtom.initialize();
        grMtom.request_item = ritmSysId;
        grMtom.sc_item_option = ritmVar.sc_item_option;
        var opid = grMtom.insert();
    }

createCatalogTask(array[i], ritmSysId, catalogItem); //create cataloag task for each ritm generated
startWorkflow('26eac8f693daa2108775396efaba10fb', ritmSysId, );
}

function startWorkflow(id, ritmSysId) {
    var grRitm1 = new GlideRecord("sc_req_item");
    grRitm1.addQuery("sys_id", ritmSysId);
    grRitm1.query();
    if (grRitm1.next()) {
        var w = new Workflow();
        var context = w.startFlow(id, grRitm1, grRitm1.operation());
        if (context != null)
            grRitm1.context = context.sys_id
    }
    grRitm1.update();
}