Attempting to add a workflow to a newly-created request item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2017 12:32 PM
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2017 02:46 PM
Hi Eric,
You don't need to attach the workflow using script. it will attach automatically
Just make sure that the workflow you have created has the right conditions for attaching itself to the item.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2017 03:50 PM
Thanks Varun, but the workflow doesn't attach automatically when the request item is created with code. The workflow that is associated with a Catalog Item would be attached if the catalog item were submitted from the Service Catalog. Unfortunately, the fact that there could be dozens of request items with hundreds of tasks makes that impractical in this situation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-26-2017 09:20 AM
HI Eric,
Check this,
May be this would help you
Attaching workflow to a RITM via script
Please mark the answer has helpful or correct based on the impact on the question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-26-2017 09:48 AM
Thanks Varun, I've found that reference, but I the half dozen iterations that I've tried to use the code hasn't worked
var w = new Workflow();
var context = w.startFlow(wid, current, current.operation(), getVars());
The versions that I've unsuccessfully tried are as follows:
//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);
I do know that current isn't something that I can call from a client script (or background script, where I'm testing this), so, while I have the ID of the workflow, I don't know what I enter in current, what I can use in current.operation, or what vars I need to enter.