Run Workflow from Client Script then retrieve value BACK before submit

Katie A
Mega Guru

I need to run a workflow from a CLIENT SCRIPT and then retrieve the result of the workflow back to the client on the form BEFORE THE FORM IS SUBMITTED.

The user will enter a desired "hostname" in a text box and an On Change client script will run a workflow with the orchestration "Test Server Alive" activity.

The result of that activity must be sent back to the client BEFORE the form is submitted. This will let the user know if the hostname is valid or not before proceeding to the next step.

I have the code that launches the workflow and passes the input value to the Test Server Alive activity.

However, how can I get the RESULT of that activity BACK to the form before it is submitted?

Here is the code that runs the workflow and passes in the user's hostname value (I am using a hard-coded string value for testing currently).

var gr = new GlideRecord('wf_workflow');

  gr.addQuery('name', 'Global Hostname Check if Taken');

  gr.query();

  if (gr.next()) {

    var wf = new Workflow();

    var workflowId = '' + gr.sys_id;

      var vars = {"u_hostname": "hostname123"};

    wf.startFlow(workflowId, current, current.operation, vars);

  }

1 ACCEPTED SOLUTION

I just tried a quick test to see what I could see and my "context" variable had a lot of goodies in there.




var wf = new Workflow()


//Get the workflow id


var   wfId = wf.getWorkflowFromName("Comprehensive Change");


//Start workflow, passing along name : value pair(s) for mapping to variable


//where input_var_name is the name of the variable declared in gear menu


//and input_var_value is whatever that value should be for this execution of


//workflow


var context = wf.startFlow(wfId, null, "Workflow Name", {"test": "test"});


gs.print("context: " + context.id);


for(x in context){


gs.print(x + "     " + context[x]);


}


View solution in original post

12 REPLIES 12

cwilkerbb
Giga Expert

You should be able to get creative with GlideAjax to accomplish this.   The code you have here would be in a Script Include and you could probably get the values back to the Script Include to pass back up to the Client.



GlideAjax - ServiceNow Wiki


Yep I am going to be using AJAX in a script include. My question is how can I possibly get the values back to the script include? That is the piece I am missing.



since the form hasn't been submitted, there is no actual record that I can reference.


So I think you'd have to get creative in your Script Include to query either the Workflow Contexts or the ECC Queue table to get your status, but I'd actually recommend looking into another path.   I'd take that code out of your Workflow and put it into a Script Include that you could call from your AJAX Script Include as well as your Workflow.   That way you're not stuck with the Workflow and it just gets light years easier to implement and maintain going forward.



-Chris


The code is in a scheduled job right now, it isn't in the workflow itself. I'm just using a scheduled job for quick testing.



How can I reference the workflow context that was run from the script?



I can query the Workflow Activity History to check the result of the Test Server Alive activity.



But, how can I retrieve the workflow context ID ahead of time in the script?



workflowactivityhistory.png