Starting workflow from Script

bernard6
Mega Expert

Hi Everyone,

I've been banging my head trying to get this to work.   I have a workflow that I have configured to return a value as the workflow.result.   I'm starting the workflow from a script using the startFlow method.   How can I have the script wait for the workflow to complete and then collect the result value?

I've tried collecting the value using this, but am unsure if it is not working because the workflow hasn't completed or because it is incorrect:

var w = new Workflow();

var context = w.startFlow(ID, null, "workflow_name",vars);

var valueReturned = context.result

Any insight would be greatly appreciated.

Thanks,
David Bernard

1 ACCEPTED SOLUTION

David,



I am not really sure how you could wait directly in that script. I know that it is possible via workflow "Wait for condition" activity, but that needs to be used within a workflow.


Another way would be to give it a sleep time via "gs.sleep(3000)" (3s pause example) , but this has no guarantee to work, especially if workflow takes longer.



Regards,


Sergiu


View solution in original post

7 REPLIES 7

David,



I am not really sure how you could wait directly in that script. I know that it is possible via workflow "Wait for condition" activity, but that needs to be used within a workflow.


Another way would be to give it a sleep time via "gs.sleep(3000)" (3s pause example) , but this has no guarantee to work, especially if workflow takes longer.



Regards,


Sergiu


Thanks Sergiu.   I ended up creating a Do... While... loop that connects and checks the state of the context and ends once the state is not 'executing'.   I used the sleep method as you suggested to add a pause in-between attempts.



Something weird I ran into is that it would not allow me use '&&' to specify another condition in the While logic.   I was looking to keep a count of the amount of times the loop had been performed so that at a certain point I could have it exit the loop if for some reason it was still running after a minute.



I'm unsure why as I believe this is accurate:   while (wf_state == 'executing' && attempts < 12 )



It's a UI Macro where I'm trying to use this.   The error I receive is:   Error at line (35) The entity name must immediately follow the '&' in the entity reference.



Any idea why it won't let use that code for the while statement?


David,



UI macros use jelly code, so you need to follow these rules for jelly code:



Extensions to Jelly Syntax - ServiceNow Wiki



Alternately, in a Jelly test you would use &amp&amp. For example:


<j:if test="${jvar_form_name == 'sys_form_template' &amp;&amp; !RP.isDialog()}">



Regards,


Sergiu