Attempting to add a workflow to a newly-created request item

ewilks
Giga Expert

I am working on a scheduled job that will allow me to create requests and request items, then under each request item I would create separate tasks under each of the request items, each to be assigned to different groups. I have been successful in creating the requests, request items and tasks. What I need is a workflow that, once all the tasks are closed, would close the request item.   To make that happen, I set up a workflow and attached it to the request item, however, workflows don't attach when creating a request item from code. So I looked for how to attach a workflow using javascript.   I found the documentation, but, despite multiple iterations I can't attach the workflow.  

Below is the code that I've used.   The requests and items are created, but the failure is when I try to attach the workflow.   You can see how I tried calling startFlow, but every version has failed.

I have added some notes/ explanation of what I've tried, so far.     Any help would be appreciated:

/*

  • workflowId - The sys_id of the workflow to start. This sys_id refers to table wf_workflow:   77dc0136db680300129d5421cf96197c is the sys_id of the workflow I want to attach
  • current - GlideRecord of the record to use as current in this workflow. This is normally from the "Table" field of the workflow properties for this workflow: **I don't believe current works form script includes, so what can I use
  • operation - The operation being performed on current. Possible values: "insert", "update", "delete" **I don't know how I can use the "insert" operation
  • vars - Collection of variables to add to the workflow ** I tried to steal some code that someone indicated that they'd used, but this did nothing.

*/

//CODE BELOW//

//creates request   --WORKS FINE

var myReq= new GlideRecord('sc_request');

myReq.initialize();

myReq.short_description = 'This is to hold the request items I will make below';

myReq.description = 'Test Request';

myReq.insert()

reqID= myReq.sys_id;

gs.print('Request ID: ' + reqID);

//creates req item under request   --WORKS FINE

var otTask = new GlideRecord('u_ot_reminder_tasks');

otTask.addQuery('short_description', 'Virus Protection');

otTask.addQuery('u_frequency', 'a4c8f5b4db200300129d5421cf9619a4');

otTask.query();

while (otTask.next()){

}

var gr = new GlideRecord('sc_req_item');

gr.initialize();

gr.short_description= 'This is an OT Task ReqItem';

gr.description = 'This reqItem needs to be completed by OT folks';

gr.request=reqID;

gr.cat_item = 'c41c0d32db680300129d5421cf961964';

gr.insert();

myRITM= gr.sys_id;

//attempting to add workflow to the newly created request item above. --DOESN'T WORK

var wkFlow = new Workflow();

var vars ={};

//below are all the iterations that I've used, none have worked.   Can anyone give me advice on how to make this work?

//var context = wkFlow.startFlow('77dc0136db680300129d5421cf96197c',myRITM, insert);

//var context = wkFlow.startFlow('77dc0136db680300129d5421cf96197c',gr, gr.operation());

//var context = wkFlow.startFlow('77dc0136db680300129d5421cf96197c',gr, gr.operation('insert'), vars);

//var context = wkFlow.startFlow('77dc0136db680300129d5421cf96197c',gr,, vars);

var context = wkFlow.startFlow('77dc0136db680300129d5421cf96197c', current, current.operation('insert'), vars);

wkFlow.insert();

16 REPLIES 16

Yes Ben,


The requested item is created from a catalog item.   Yes, the catalog item has a workflow attached to it (see attached), but it still no workflow automatically attaches, like I figured it would.


Thanks,


Eric


find_real_file.png


Community Alums
Not applicable

If you create this item in the Catalog, does the Workflow properly attach then? What is your Scheduled Job running to create the Requested Item? I do this quite often via a script in a Workflow. You can see how in my blog post, Get General with Your Requests.


 


I have since found the documentation for the API used: https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=GlideappCalcHelperAPI