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

Move it from the Scheduled Job into a Script Include and then call THAT from your Scheduled Job for testing.   You'd have to play around with some more complicated coding to get the Context..probably get it from the wf.startWorkflow() command, but I wouldn't bother going down that path since you'd be testing a scenario that is not recommended.



-Chris


Thanks for your help. What would be the recommended method to do this in that case?


So you'd take whatever is in 'Global Hostname Check if Taken' that you would use and create a Script Include using the GlideAjax functionality.   Create that function something like:



function checkHostname(){


var hostname = this.getParameters('my_hostname');


//do what you need to with the values


return true;


}



Then you can use the GlideAjax framework to evaluate "true" and do what you need to do.   If you needed to have the code in your Workflow then you would just have AJAXScriptIncludeName.checkHostname(); as the function call in that Run Script.   This will allow you to manage that code from one place and be able to use it anywhere you want since Script Includes are basically similar to Java Classes.  



-Chris




Thanks Chris. I am planning on using the GlideAjax structure.



The issue I am having is that I am unable to retrieve the Context of the workflow that is running to retrieve the result of the Test Server Alive activity.



I am experimenting with wf.getContexts(current). This returns a GlideObject but I am unable to dot-walk it to retrieve the relevant sys_ids.



I basically just need the context of the currently running workflow so that I can query the Workflow Activity History and check the result of the Test Server Alive activity.



workflowactivityhistory.png


You can try JSUtil.logObject(wf) and see if that helps.   What is in the Workflow that you can't pull out and put into a Script Include?   I'm at a loss as to why you're so determined to call a Workflow here.



-Chris