Work order close completing before workflow can complete.

cpinedatx94
Tera Expert

I have a workflow that is set to make certain task based on the prior task going to close complete. The issue is the work order is completing itself because the workflow isnt allowing time for the second task to be made before close completing. Is there a way we can fix this.

 

Thanks

1 REPLY 1

Ramesh Lohar
Kilo Guru

Yes, there are several ways to fix this issue. Here are some steps you can take:

1. **Add a Timer Activity**: You can add a timer activity in your workflow after the first task is completed. This will delay the workflow from moving to the next activity until the timer expires. This can give enough time for the second task to be created.

Sample Code:

var timer = new Workflow().getTimerDuration('1 00:00:00'); // 1 day delay
current.workflow_timer = timer;
current.update();

2. **Use 'Wait for condition' Activity**: You can use the 'Wait for condition' activity in your workflow. This activity pauses the workflow until a certain condition is met. In your case, you can set the condition to wait until the second task is created.

Sample Code:

var wf = new Workflow();
var context = wf.startFlow('your_workflow_sys_id', current, 'update');
wf.waitForCmdbCiCondition(current, 'state=3', context); // Wait until state of the task is 'Closed Complete'

3. **Use Business Rules**: You can create a business rule that prevents the work order from being completed if the second task is not created. This business rule can be set to run before the work order is updated to 'Closed Complete'.

Sample Code:

if (current.state == '3' && !second_task_created) { // If state is 'Closed Complete' and second task is not created
current.setAbortAction(true); // Prevent the record from being updated
gs.addErrorMessage('Second task is not created yet.');
}

4. **Use Scripted REST APIs**: If the second task is created by an external system, you can use Scripted REST APIs to create the second task and then update the work order to 'Closed Complete'. This ensures that the second task is created before the work order is completed.

Sample Code:

var gr = new GlideRecord('task_table');
gr.initialize();
gr.short_description = 'Second Task';
gr.insert();

var wo = new GlideRecord('work_order_table');
wo.get('work_order_sys_id');
wo.state = '3'; // 'Closed Complete'
wo.update();

Remember to replace 'your_workflow_sys_id', 'task_table', 'work_order_table', and 'work_order_sys_id' with your actual sys_ids and table names.

 

Hello All,
For ServiceNow Live Classes, Books, Sample Resumes, Interview Questions, CSA Quizzes. And getting better servicess on ServiceNow you can visits our website.
Please visit : https://nowkb.com/home
Our Website :https://nowkb.com/home
nowKB.com