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

Thanks Chris! I am using a Workflow because I need to run the Orchestration activity "Test Server Alive" to check if the user's hostname is already taken before they submit the form.



https://community.servicenow.com/thread/216450


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]);


}


I was able to use this line to pull the sysid of the context. Now, I can query the context result of the activity in the same script include. Thanks!!!!!!!!



var context = wf.startFlow(wfId, current, current.operation, vars);
gs.log("CONTEXT: " + context.sys_id);