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

Thanks for all the help Varun.   I'm going to give up on this, and submit the reminders on paper  


I'm off to smash my head into the refrigerator in the break room


Tagging Chuck Tomasi ctomasi to help out on this


The key I'm picking up from the original message is this:


What I need is a workflow that, once all the tasks are closed, would close the request item.


That speaks Business Rule to me. This logic isn't part of the RITM workflow since that only gets up dated when the RITM gets updated. You want a "close" operation on the task to trigger the RITM to close. See the disconnect there? That's why the BR runs on the sc_task table to say "Hey, I've been closed, are all my brothers and sisters closed too? If so, then close the parent RITM record."



Make sense? If so, the BR would look something like this:


Standard disclaimer: The following code is untested, requires review and potential modifications.



Name: Check all child tasks closed


Table: Catalog task (sc_task)


Insert: true


Update: true


Advanced: true


When: After


Condition: current.active.changesTo(false)



Script:


(function executeRule(current, previous /*null when async*/) {



      var rec = new GlideAggregate('sc_task');


      rec.addQuery('request_item', current.getValue('request_item'));


      rec.addQuery('active', true);


      rec.addAggregate('COUNT');


      rec.query();



      if (rec.next()) {


                  var count = rec.getAggregate('COUNT');


                  if (count == 0) {


                            var ritm = new GlideRecord('sc_req_item');


                              if (ritm.get(current.getValue('request_item')) {


                                      ritm.state = 7;


                                      ritm.update();


                            }


                  }


      }



})(current, previous);


First of all, thank you very much, Chuck and Varun. You are both appreciated.


I started to work with this and with the Business Rule suggestion by Chuck, I ended up having the request item close before any of the tasks were added.   I'm going to see what I can do to fix that before I come back an ask for more assistance from you guys.   I'm sure there is something I can do to remedy that issue.


Community Alums
Not applicable

Eric,



At the risk of sounding dense, and not exploring all the previous responses in detail... Is your Requested Item that you create from the Scheduled job based on a Catalog Item? If so, you could just attach your Workflow to the Catalog Item from which you are generating the Requested Item? Then you wouldn't need to futz around with any of this and you'd have an easier way to manage the workflow used by future items. You could also have the workflow generate the tasks associated with the Requested Item, too.



Perhaps I a missing something here, though...



Ben