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

try this



var wid = '77dc0136db680300129d5421cf96197c';


startWorkflow(wid);




function startWorkflow(wid)


{


    var w = new Workflow();


    var context = w.startFlow(wid, myRITM, myRITM.operation(), getVars());


    if (context != null)


          {


          current.context = context.sys_id;


          current.update();


   


    }


}


function getVars()


{



    var vars = {};


    for (var n in myRITM.variables)


          {


          vars[n] = myRITM.variables[n];


    }


    return vars;


}


Varun,


When I run the script, I get the following errors:


Evaluator: org.mozilla.javascript.EcmaError: Cannot set property "context" of null to "b1945040db690300129d5421cf96190d"


  Caused by error in script at line 35



  32: var context = w.startFlow(wfId, myRITM, myRITM.operation(), getVars());


  33: if (context != null)


  34: {


==> 35: current.context = context.sys_id;


  36: current.update();


  37:  


  38: }



Evaluator: org.mozilla.javascript.EcmaError: Cannot set property "context" of null to "b1945040db690300129d5421cf96190d"


  Caused by error in script at line 29



  26: myRITM= gr.sys_id;


  27:


  28: var wfId = '77dc0136db680300129d5421cf96197c';


==> 29: startWorkflow(wfId);


  30: function startWorkflow(wfId){


  31: var w = new Workflow();


  32: var context = w.startFlow(wfId, myRITM, myRITM.operation(), getVars());




Below is the script that I ran.   I think it may be that I'm trying to use current in a background script:


//creates request


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.cat_item = 'c41c0d32db680300129d5421cf961964';


myReq.insert()


reqID= myReq.sys_id;


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




//creates req item under request


var otTask = new GlideRecord('u_ot_reminder_tasks');


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


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


otTask.query();


while (otTask.next()){


gs.print(otTask.short_description);


}


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;




var wfId = '77dc0136db680300129d5421cf96197c';


startWorkflow(wfId);


function startWorkflow(wfId){


    var w = new Workflow();


    var context = w.startFlow(wfId, myRITM, myRITM.operation(), getVars());


    if (context != null)


                {


                current.context = context.sys_id;


                current.update();


       


    }


}




function getVars(){


    var vars = {};


    for (var n in myRITM.variables)


          {


          vars[n] = myRITM.variables[n];


    }


    return vars;


}


change


current.context = context.sys_id;


                current.update();



to



myRITM.context = context.sys_id;


                myRITM.update();


Thanks Varun,


I tried that and while the request and request item were created, there is no workflow attached to the request item.



//creates request (THIS WORKS)


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.cat_item = 'c41c0d32db680300129d5421cf961964';


myReq.insert()


reqID= myReq.sys_id;


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




//creates req item under request (THIS WORKS)


var otTask = new GlideRecord('u_ot_reminder_tasks');


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


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


otTask.query();


while (otTask.next()){


gs.print(otTask.short_description);


}


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;



//Adding the workflow (THIS DOESN"T WORK)


var wfId = '77dc0136db680300129d5421cf96197c';


startWorkflow(wfId);


function startWorkflow(wfId){


    var w = new Workflow();


    var context = w.startFlow(wfId, myRITM, myRITM.operation(), getVars());


    if (context != null)


                {


                myRITM.context = context.sys_id;


                myRITM.update();


       


    }


}




function getVars(){


    var vars = {};


    for (var n in myRITM.variables)


          {


          vars[n] = myRITM.variables[n];


    }


    return vars;


}


Hi Eric,



You would have already tried it , but just wanted to be sure did you try myRITM.insert(); instead of myRITM.update();