Flow on cat item is not triggering when created via a scheduled job

riskay123
Mega Guru

Hello,

I have a catalog item that uses a flow. When I submit the catalog item manually the flow behaves as expected.  I eventually want to be able to schedule a job that runs that uses this catalog item/flow. My question is regarding the scheduled job. I am using a PDI Instance on Washington.

 

I am using the below script to test the scheduled job and it is creating the REQ and the RITM fine but does not kick off the flow that is assigned as the process engine for this catalog item.  I would expect the flow to update the State, Stage, Assignment group, Short Description and trigger a SCTASK. What do I need to amend in the script so the flow will also trigger. The trigger condition on the flow is currently set to "User who initiates session". I have tried swapping it to "System User" but that didn't seem to make any difference. I am not a coder so please be kind (dumb it down) with your explanation 🙂

 

/ Scheduled Job Script
// This script runs periodically to create REQ and RITM records

// Define the catalog item's sys_id (replace with your actual catalog item sys_id)
var catalogItemSysId = '396e5135970302101329f6fce053af26';

// Create a new Request (REQ) record
var req = new GlideRecord('sc_request');
req.initialize();
req.requested_for = 'Scheduled_Job_User'; // Set the requested_for user
req.short_description = 'Reset Kerberos Password'; // Set the request description
req.insert();

// Create a new Requested Item (RITM) record
var ritm = new GlideRecord('sc_req_item');
ritm.initialize();
ritm.requested_for = 'Scheduled_Job_User'; // Set the requested_for user
ritm.request = req.sys_id; // Link RITM to the REQ
ritm.cat_item = catalogItemSysId; // Set the catalog item
ritm.insert();

gs.info('REQ created: ' + req.number);
gs.info('RITM created: ' + ritm.number);
 

Also, the Stages don't appear to be correct when the scheduled job runs. I am assuming this is because the flow is not running that has the Stages defined. If I am on the wrong track about that I would appreciate someone letting me know. The bottom one is manually run and is correct. The top one is from the scheduled job. 

 

Stage.png

3 REPLIES 3

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Have to reproduce. Though first thing I immediately notice, is that you are "manually" creating the request, requested item and I don't see any variables?

 

Have you considered using the CartJS instead? Way easier and more reliable.

https://docs.servicenow.com/bundle/washingtondc-api-reference/page/app-store/dev_portal/API_referenc...

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Tai Vu
Kilo Patron
Kilo Patron

Hi @riskay123 

Let give the CartJS a try.

var cart = new sn_sc.CartJS();
var request =
{
  'sysparm_id': '42151881475eb910ab9bb6bf016d4308', //catalog item sys_id
  'sysparm_quantity': '1', //quantity
  //variables
  'variables':{
    'requested_for': '6816f79cc0a8016401c5a33be04be441',
  }
};
var cartDetails = cart.orderNow(request);

 

Cheers,

Tai Vu

 

 

@riskay123 

Or you can also consider to add this line to your script.

sn_flow_trigger.FlowTriggerAPI.fireCatalogTrigger('<flow_internal_name>', ritm);

 

Ref:

Business Rule: Start FlowDesigner Flow

URL: https://<instance_name>.service-now.com/sys_script.do?sys_id=837a49f63ba013008ed00d8044efc4e5

 

Cheers,

Tai Vu