The CreatorCon Call for Content is officially open! Get started here.

Calling workflow from script

agulati
Kilo Expert

Is there a way to call a workflow using script?I want the workflow to be only called from script and not by default when record is created for   a table.

12 REPLIES 12

Let me paraphrase the question for you:



You want to re-attach a new workflow when the assignment team changes?



Because, I am not entirely sure what you mean by "call" the workflow.


Yes.


1. I have created a workflow on incident and that gets called when a new Incident is created and then i have a timer activity in the workflow   to wait for few minutes.


2. The issue is that now when i update the incident with work notes or any other field changes, i see that there are 2 updates. and If i check active workflow context, i see a new activity for the timer for a particular incident.


3.Is there a way to prevent this from happening or any updates


4. The only exception is that when the assignment group is changed, i want to cancel the first workflow and re- attach a new workflow


Hi Aman,



I have a question about the purpose of the workflow. There may be another function in servicenow that can solve your solution. If you are looking to remind the assignment group about an incident you could possibly use the Inactivity Monitor Setting Inactivity Monitors - ServiceNow Wiki



Also if you are looking to monitor the responsiveness of an assignment group you could possibly use the SLA engine with an OLA. Defining an SLA - ServiceNow Wiki



Lastly there is a metric instance that is created (by default) for each change of assignment group on an Incident which might give you an option for your workflow.



Hope this helps.


Cheers,


Cameron


adiddigi
Tera Guru

I got you.



Two steps here:



1. To cancel the existing workflow you have to use:



var wf = new Workflow();
    var flows = wf.getRunningFlows(current);
   
    while(flows.next()){
          wf.cancelContext(flows);
    }

and then, re-attach the new workflow you will have to use :



//get workflow sysid


var wid = '1a80e108b8e1010042fa8fe93cec2e4d';


startWorkflow(wid);




function startWorkflow(id)


{


    var w = new Workflow();


    var context = w.startFlow(id, current, current.operation(), getVars());


    if (context != null)


          {


          current.context = context.sys_id;


          current.update();


         


    }


}


function getVars()


{


   


    var vars = {};


    for (var n in current.variables)


          {


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


    }


    return vars;


}


Thanks Abhiram.



Even after adding these, i see the issue with updates happening twice.


The One peculiar thing about this workflow is that it has a timer and workflow is waiting on that timer when i am making other updates to the incident.


Do you know if that would cause the updates to happen multiple times.



Thanks