- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2015 07:53 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2015 11:32 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2015 08:40 AM
Hi David,
As far as I know you can't catch the workflow result.
The "startFlow" method only inserts a record in table wf_context for the newly created workflow context. For more information see here:
Workflow Script - ServiceNow Wiki
You can see in the wiki link the following:
3.2.2 Output Fields
Returns: A GlideRecord on table wf_context on the inserted record for this newly created workflow context.
So the "context" variable in your case is just a GlideRecord object on the table wf_context, but this has no relation with workflow result.
Regards,
Sergiu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2015 09:44 AM
Hi Sergio,
The wf_context table has a result column. When I navigate to the context table: https://instance/wf_context_list.do I can see the 'instance / context' of the workflow that I started and the result was properly populated with a value from the workflow.
By using gs.log() I can see that the state is 'executing' when I attempt to retrieve the result value. So I tried to do a while loop to have the script wait until the state was anything but 'executing' - while (context.state == 'executing') { }. Though that resulted in an endless loop.
Is there a better way to have the script wait for the workflow to finish?
Thanks,
David
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2015 10:30 AM
Yes, there is a result field indeed, but on my test instance with out of box configuration for my finished workflows the field is not populated, stays empty.
Are we talking about a customized workflow? Are you sure it is going to finish fast? And, on top of that is the result populated in the result field?
As an example a workflow for a change request approval could be in "executing" for hours, even not days, until the approver either approvers or rejects the change. A script waiting for such an workflow is probably not a good idea.
What is your aim with such a script?
Regards,
Sergiu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2015 10:53 AM
It is a custom workflow and the result is populated into the result field. The workflow is performing a query via a MID server to return a value from a file. My long term goal it have it pull that value from a DB via the MID server.
I'm attempting to create my own 'Personal Verification Confirmation Type' 'Verification Type' for Password Reset where the answer will be provided from an on-premise DB, instead of the 'sys_user' table. The value will be displayed to the Service Desk analyst who will confirm the answer with the customer.
I would expect that the workflow would not take too long to execute.