We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Trying to copy Agile Story and associated scrum tasks in UI Action

Stan37
Giga Contributor

Hi,

I am trying to create a UI Action that copies an Agile Story and all associated Scrum tasks.  I can get the Story to create with no issue, but can't seem to generate the scrum tasks.  I have tried several variations of this script (I am new to javascript).  This is essentially a copy from SN guru (just changing the table names).

 

//copy Story is working
var chgID = current.sys_id.toString();
gs.addInfoMessage('sysid ' + chgID + ' copy story.');
var newChange = new GlideRecord('rm_story');
newChange.initialize();
newChange.short_description = current.short_description;
newChange.assignment_group = current.assignment_group;
newChange.description = current.description;
newChange.insert();

//Copy attachments for this change - this is working
if (typeof GlideSysAttachment != 'undefined')
GlideSysAttachment.copy('rm_story', chgID, 'rm_story', newChange.sys_id);
else
Packages.com.glide.ui.SysAttachment.copy('rm_story', chgID, 'rm_story', newChange.sys_id);
copyTask();
//copyCI(); don't need
gs.addInfoMessage('Story ' + newChange.number + ' created.');
action.setRedirectURL(newChange);

//copy scrum tasks is not working
function copyTask() {
//Find the current scrum tasks and copy them
var tasks = new GlideRecord('rm_scrum_task');

tasks.addQuery('rm_story', chgID);

gs.addInfoMessage('sysid ' + chgID + ' copyTask.');
tasks.query();
while (tasks.next()) {
var taskID = tasks.sys_id.toString();
//Copy the task record to a new task record
var newTask = new GlideRecord('rm_scrum_task');
newTask.initialize();
newTask.story = newChange; //this is a reference - is this correct?
newTask.short_description = tasks.short_description;
newTask.description = tasks.description;
newTask.assignment_group = tasks.assignment_group;
newTask.assigned_to = tasks.assigned_to;
newTask.insert();


//Copy attachments for this task - this is not working since scrum tasks are not copying
if (typeof GlideSysAttachment != 'undefined')
GlideSysAttachment.copy('rm_scrum_task', taskID, 'rm_scrum_task', tasks.sys_id);
else
Packages.com.glide.ui.SysAttachment.copy('rm_scrum_task', taskID, 'rm_scrum_task', tasks.sys_id);
}
}
//turning off all the below
//function copyCI() {
//Copy over the affected CI list
//var currentCI = new GlideRecord('task_ci');
//currentCI.addQuery('task', current.sys_id);
//currentCI.addNullQuery('u_ci_group'); //Added to ensure that copying does not duplicate Group CIs
//currentCI.query();
//while(currentCI.next()){
//var newCI = new GlideRecord('task_ci');
//newCI.initialize();
//newCI.task = newChange.sys_id;
//newCI.ci_item = currentCI.ci_item;
//newCI.insert();
//}
//}

1 ACCEPTED SOLUTION

Hi Stan,

Did the same on my pdi, adjusted result below;

//copy Story is working
var chgID = current.sys_id.toString();
var newChange = new GlideRecord('rm_story');
var newStorySysId;
newChange.initialize();
newChange.short_description = current.short_description;
newChange.assignment_group = current.assignment_group;
newChange.description = current.description;
newStorySysID = newChange.insert();
//newChange.insert();

if (newStorySysID){
	//Copy attachments for this change - this is working
	if (typeof GlideSysAttachment != 'undefined')
		GlideSysAttachment.copy('rm_story', chgID, 'rm_story', newStorySysID);
	else
		Packages.com.glide.ui.SysAttachment.copy('rm_story', chgID, 'rm_story', newStorySysID);
	copyTask(newStorySysID,chgID);
	//copyCI(); don't need
	gs.addInfoMessage('Story ' + newChange.number + ' created.');
	action.setRedirectURL(newChange);
}


//copy scrum tasks is not working
function copyTask(ID,chgID) {
	//Find the current scrum tasks and copy them
	var tasks = new GlideRecord('rm_scrum_task');
	tasks.addQuery('story', chgID);
	tasks.query();
	while (tasks.next()) {
		var taskID = tasks.sys_id.toString();
		gs.addInfoMessage('task sys_id ' + taskID + ' task.');
		//Copy the task record to a new task record
		var newTask = new GlideRecord('rm_scrum_task');
		newTask.initialize();
		newTask.story = ID;
		newTask.short_description = tasks.short_description;
		newTask.description = tasks.description;
		newTask.assignment_group = tasks.assignment_group;
		newTask.assigned_to = tasks.assigned_to;
		newTask.insert();
		
		//Copy attachments for this task - this is not working since scrum tasks are not copying
		if (typeof GlideSysAttachment != 'undefined')
			GlideSysAttachment.copy('rm_scrum_task', taskID, 'rm_scrum_task', tasks.sys_id);
		else
			Packages.com.glide.ui.SysAttachment.copy('rm_scrum_task', taskID, 'rm_scrum_task', tasks.sys_id);
	}
}

View solution in original post

4 REPLIES 4

Roel3
Mega Guru

Hi Stan,

The error is in your query (at least if I look at Madrid version);

tasks.addQuery('rm_story', chgID);

should be;

tasks.addQuery('story', chgID);

 

On top of that i would use a variable on the insert;

var newStorySysId;

newStorySysId = newChange.insert();

and use that when creating task;

newTask.story =newStorySysId;

Regards Roel

 

Stan37
Giga Contributor

Hi Roel,

Thank you for the help! I am on Madrid and testing this in my PDI.

 

I made the suggested changes, though I am not sure I have the syntax correct.  I still see the same behavior in that the Story is created but not the child scrum tasks.

 

/copy Story is working
var chgID = current.sys_id.toString();
gs.addInfoMessage('sysid ' + chgID + ' copy story.');
var newChange = new GlideRecord('rm_story');
var newStorySysId;
newChange.initialize();
newChange.short_description = current.short_description;
newChange.assignment_group = current.assignment_group;
newChange.description = current.description;
newStorySysID = newChange.insert();
//newChange.insert();

//Copy attachments for this change - this is working
if (typeof GlideSysAttachment != 'undefined')
GlideSysAttachment.copy('rm_story', chgID, 'rm_story', newChange.sys_id);
else
Packages.com.glide.ui.SysAttachment.copy('rm_story', chgID, 'rm_story', newChange.sys_id);
copyTask();
//copyCI(); don't need
gs.addInfoMessage('Story ' + newChange.number + ' created.');
action.setRedirectURL(newChange);

//copy scrum tasks is not working
function copyTask() {
//Find the current scrum tasks and copy them
gs.addInfoMessage('copy task.');
var tasks = new GlideRecord('rm_scrum_task');
gs.addInfoMessage('scrum task.');
tasks.addQuery('story', chgID);
//gs.addInfoMessage('scrum query.');
gs.addInfoMessage('sysid ' + chgID + ' copyTask.');
tasks.query();
while (tasks.next()) {
var taskID = tasks.sys_id.toString();
gs.addInfoMessage('task sys_id ' + taskID + ' task.');
//Copy the task record to a new task record
var newTask = new GlideRecord('rm_scrum_task');
newTask.initialize();
newTask.story = newStorySysId;
newTask.short_description = tasks.short_description;
newTask.description = tasks.description;
newTask.assignment_group = tasks.assignment_group;
newTask.assigned_to = tasks.assigned_to;
newTask.insert();


//Copy attachments for this task - this is not working since scrum tasks are not copying
if (typeof GlideSysAttachment != 'undefined')
GlideSysAttachment.copy('rm_scrum_task', taskID, 'rm_scrum_task', tasks.sys_id);
else
Packages.com.glide.ui.SysAttachment.copy('rm_scrum_task', taskID, 'rm_scrum_task', tasks.sys_id);
}
}
//turning off all the below
//function copyCI() {
//Copy over the affected CI list
//var currentCI = new GlideRecord('task_ci');
//currentCI.addQuery('task', current.sys_id);
//currentCI.addNullQuery('u_ci_group'); //Added to ensure that copying does not duplicate Group CIs
//currentCI.query();
//while(currentCI.next()){
//var newCI = new GlideRecord('task_ci');
//newCI.initialize();
//newCI.task = newChange.sys_id;
//newCI.ci_item = currentCI.ci_item;
//newCI.insert();
//}
//}

 

Hi Stan,

Did the same on my pdi, adjusted result below;

//copy Story is working
var chgID = current.sys_id.toString();
var newChange = new GlideRecord('rm_story');
var newStorySysId;
newChange.initialize();
newChange.short_description = current.short_description;
newChange.assignment_group = current.assignment_group;
newChange.description = current.description;
newStorySysID = newChange.insert();
//newChange.insert();

if (newStorySysID){
	//Copy attachments for this change - this is working
	if (typeof GlideSysAttachment != 'undefined')
		GlideSysAttachment.copy('rm_story', chgID, 'rm_story', newStorySysID);
	else
		Packages.com.glide.ui.SysAttachment.copy('rm_story', chgID, 'rm_story', newStorySysID);
	copyTask(newStorySysID,chgID);
	//copyCI(); don't need
	gs.addInfoMessage('Story ' + newChange.number + ' created.');
	action.setRedirectURL(newChange);
}


//copy scrum tasks is not working
function copyTask(ID,chgID) {
	//Find the current scrum tasks and copy them
	var tasks = new GlideRecord('rm_scrum_task');
	tasks.addQuery('story', chgID);
	tasks.query();
	while (tasks.next()) {
		var taskID = tasks.sys_id.toString();
		gs.addInfoMessage('task sys_id ' + taskID + ' task.');
		//Copy the task record to a new task record
		var newTask = new GlideRecord('rm_scrum_task');
		newTask.initialize();
		newTask.story = ID;
		newTask.short_description = tasks.short_description;
		newTask.description = tasks.description;
		newTask.assignment_group = tasks.assignment_group;
		newTask.assigned_to = tasks.assigned_to;
		newTask.insert();
		
		//Copy attachments for this task - this is not working since scrum tasks are not copying
		if (typeof GlideSysAttachment != 'undefined')
			GlideSysAttachment.copy('rm_scrum_task', taskID, 'rm_scrum_task', tasks.sys_id);
		else
			Packages.com.glide.ui.SysAttachment.copy('rm_scrum_task', taskID, 'rm_scrum_task', tasks.sys_id);
	}
}

Stan37
Giga Contributor

Roel, 

Thank you very much! This did the trick.  I appreciate the assist.

 

Stan