Creating Problem Tasks Automatically

KeithM2
Kilo Contributor

I'm currently working in the Aspen release of SN and I'm trying to automatically create 2 tasks when a new problem is submitted. I've tried using a workflow but that didn't work. I tried an onSubmit client script and that doesn't seem to be working either. Below is the client script I setup:


function onSubmit() {
//This script creates 2 new problem tasks
var newtask1 = new GlideRecord('task');
newtask1.initialize();
newtask1.short_description = 'Create Root Cause Analysis (RCA)';
newtask1.assigned_group = 'Problem Mgmt';
newtask1.assigned_to = '1. Queue Coordinator';
newtask1.insert();

var newtask2 = new GlideRecord('task');
newtask2.initialize();
newtask2.short_description = 'Create Problem and Resolution Plan';
newtask2.assigned_group = 'Problem Mgmt';
newtask2.assigned_to = '1. Queue Coordinator';
newtask2.insert();
}

Thanks to all who have suggestions.

8 REPLIES 8

KeithM2
Kilo Contributor

I've taken the advise of everyone and created the following Business Rule:

Name: Automatic Task Creation
Table: Problem
When: After
Insert

Script:
newTasks();
function newTasks() {
if (current.sys_id == '')
{
return;
}

var newtask1= new GlideRecord('problem_task');
newtask1.addQuery('parent', current.sys_id);
newtask1.query();
while (newtask1.next()) {
//Insert Child tasks here
newtask1.short_description = 'Create Root Cause Analysis (RCA)';
newtask1.assigned_group = 'AIGGS-Problem Mgmt';
newtask1.assigned_to = '1. Queue Coordinator';
newtask1.insert();
}
var newtask2= new GlideRecord('problem_task');
newtask2.addQuery('parent', current.sys_id);
newtask2.query();
while (newtask2.next()) {
newtask2.short_description = 'Create Problem and Resolution Plan';
newtask2.assigned_group = 'AIGGS-Problem Mgmt';
newtask2.assigned_to = '1. Queue Coordinator';
newtask2.insert();
}
}


The problem is that it still is not creating the tasks. Any other suggestions or can you see anything wrong with my code?
Thanks again!


If you are creating new tasks you don't want to do a query:

var newtask1= new GlideRecord('problem_task');
newtask1.initialize();
newtask1.parent = current.sys_id;
newtask1.short_description = 'Create Root Cause Analysis (RCA)';
newtask1.assigned_group = 'AIGGS-Problem Mgmt';
newtask1.assigned_to = '1. Queue Coordinator';
newtask1.insert();
}


You might try in a RunScript step in workflow?

That code works in Dem013 here if it helps to see it:
https://demo013.service-now.com/nav_to.do?uri=problem.do?sysparm_stack=problem_list.do&sys_id=-1

[Would attach an update set but can't seem to find that option any longer; will PM you if that would help.]


DChandlerKy
Tera Guru

I was able to get this to work with a few edits.   Here is a copy of the script in our business rule in case it helps anyone else.



When: after


Insert: <checked>



newTasks();


function newTasks() {


if (current.sys_id == '')


{


return;


}




var newtask1= new GlideRecord('<problem task table>');


newtask1.initialize();


newtask1.u_problem = current.sys_id;   //This line would copy the number from the Problem parent record to the appropriate field in the child problem task record


newtask1.short_description = '<enter short description of choice>';


newtask1.assigned_group = 'Problem Management'; //Enter desired group


newtask1.insert();




}



Copy lines 9-14 as many times as necessary to create the number of tasks that you need.   For one instance, I have 7 tasks being created in one business rule for problem records with a specific category.