start a workflow from script action

gs53
ServiceNow Employee
ServiceNow Employee

Hello,

 

I have a script action which starts a workflow. The script action gets triggered by a business rule. The problem is when the script action starts the workflow the inputs are empty in the workflow. Even though i am passing the inputs. It works in script backgrounds. Anything i am missing here ?

below is the script action code.

 

(function(event, workflow_context_id, inputs) {
// Restart workflow using the provided inputs
gs.log("status_update_restart_automation script action "+event.parm2);

var workflowContextId = event.parm1;
var workflowInputs = event.parm2;

// Check if the inputs are provided
if (!workflowContextId || !workflowInputs) {
gs.error('Workflow context ID or inputs not provided.');
return;
}

// Get the workflow context
var grWorkflowContext = new GlideRecord('wf_context');
if (!grWorkflowContext.get(workflowContextId)) {
gs.error('Invalid workflow context ID: ' + workflowContextId);
return;
}
 
if (grWorkflowContext.getValue('state') === 'finished') {
var workflowSysId = grWorkflowContext.getValue('workflow');

// Get the workflow definition
var grWorkflow = new GlideRecord('wf_workflow');
if (!grWorkflow.get(workflowSysId)) {
gs.error('Unable to get workflow record.');
return;
}

// Cancel the completed workflow
var workflowCancellation = new GlideRecord('wf_workflow');
if (!workflowCancellation.get(workflowSysId)) {
gs.error('Unable to get workflow record.');
return;
}

var currentWorkflowState = workflowCancellation.workflow_state;
workflowCancellation.cancelWorkflow();

var inputs = {};
inputs.u_client_dc = workflowInputs["u_client_dc"];
inputs.u_query = workflowInputs["u_query"];

// Start a new instance of the same workflow with provided inputs
var workflowStart = new Workflow().startFlow(workflowSysId, null, null, inputs);

// Log the restart details
gs.info('Workflow restarted successfully.');
} else {
gs.error('The workflow is not in a completed state and cannot be restarted.');
}

})(event, event.parm1, event.parm2);
2 REPLIES 2

Rajdeep Ganguly
Mega Guru


Based on the script you provided, here are some potential issues and solutions:

- The workflow inputs are not being passed correctly. Ensure that the inputs are being passed as an object with the correct properties.

- The workflow is not in a 'finished' state. The script checks if the workflow is in a 'finished' state before restarting it. If the workflow is not in a 'finished' state, it will not be restarted.

- The workflow context ID or inputs are not provided. The script checks if the workflow context ID and inputs are provided. If they are not, the script will not execute.

- The workflow record cannot be retrieved. The script attempts to retrieve the workflow record using the workflow context ID. If the record cannot be retrieved, the script will not execute.

Here are the steps to troubleshoot:

1. Check the inputs being passed to the script action. They should be an object with the correct properties.

2. Check the state of the workflow. It should be in a 'finished' state for the script to restart it.

3. Ensure that the workflow context ID and inputs are being provided to the script action.

4. Check if the workflow record can be retrieved using the workflow context ID.

5. If the workflow is not in a 'finished' state, you may need to manually set it to 'finished' before running the script.

6. If the workflow record cannot be retrieved, check the workflow context ID and ensure it is correct.

7. If the inputs are not being passed correctly, you may need to adjust the script to correctly handle the inputs.

8. If all else fails, you may need to debug the script to identify the issue. Use the gs.log() function to log variables and check their values at runtime.


nowKB.com

gs53
ServiceNow Employee
ServiceNow Employee

The script is starting the workflow. In the workflow its inputs are empty even though i am passing the correct inputs in script action. I want to understand why the inputs are empty.

when i run the script from script backgrounds, it starts the workflow correctly.