Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Copying Feature, Stories, Tasks and Attachments

Blaze Llanos
Kilo Expert

I am trying to copy Feature with all Stories, Tasks and Attachments from the Safe Application
I have a field on the Feature form called u_copy_storires with the UI Action conditoin of current.u_copy_stories!=""


I can get the Feature to copy over. 

Stories will count how many are related to the feature, but copies the first one multiple time 
For example - Feature has 5 Stories. 
I can copy the Feature, the 5 unique new stories will be created, but only the first one is actually copied into the other 5. 
I can not get tasks to populate at all and attachments are not coming over. 

This is the UI Action that I have 

Copy Feature and Stories
sn_safe_feature
Active
Show Insert
Show Update
Form Button
Condition: current.u_copy_stories!=""

//New Feature
var newFeature = new GlideRecord('sn_safe_feature');
newFeature.initialize();
    newFeature.priority = current.priority;
    newFeature.sn_safe_epic = current.sn_safe_epic;
    newFeature.sn_safe_program = current.sn_safe_program;
    newFeature.priority = current.priority;
    newFeature.type = current.type; 
    newFeature.demand = current.demand; 
    newFeature.u_documentation_url = current.u_documentation_url;
    newFeature.short_description = current.short_description;
    newFeature.assignment_group = current.assignment_group;
    newFeature.description = current.description;

    var newFeatureID = newFeature.insert();
GlideSysAttachment.copy('sn_safe_feature', current.sys_id, 'sn_safe_story', newFeature.sys_id);
(current, previous);

//Retrieve Stories
var stories = new GlideRecord('sn_safe_story');
stories.addQuery('sn_safe_feature', current.getUniqueValue());
if (current.u_copy_stories=='Not Completed') {
    stories.addQuery('state', '!=',3);
}
stories.query();
gs.info("STORIES RETURNED:{0}", stories.getRowCount());
while (stories.next()){
    var newStory = new GlideRecord('sn_safe_story');
    newStory.initialize();
    newStory.sn_safe_feature = newFeatureID;
    newStory.sn_safe_epic = current.sn_safe_epic;
    newStory.sn_safe_program = current.sn_safe_program;
    newStory.priority = current.priority;
    newStory.type = current.type; 
    newStory.demand = current.demand; 
    newStory.u_documentation_url = current.u_documentation_url;
    newStory.short_description = current.short_description;
    newStory.assignment_group = current.assignment_group;
    newStory.description = current.description;
    
    var newStorySysID = newStory.insert();
    
GlideSysAttachment.copy('sn_safe_story', current.sys_id, 'sn_safe_story', newStory.sys_id);
(current, previous);
}

//Retrieve Tasks
var tasks = new GlideRecord('sn_safe_scrum_task');
stories.addQuery('sn_safe_story', current.getUniqueValue());
if (current.u_copy_stories=='Not Completed') {
    stories.addQuery('state', '!=',3);
}
tasks.query();
gs.info("TASKS RETURNED:{0}", tasks.getRowCount());
while (stories.next()){
    var newTask = new GlideRecord('sn_safe_scrum_task');
    newTask.initialize();
    newTask.sn_safe_story = newStorySysID;
    newTask.priority = current.priority;
    newTask.type = current.type;
    newTask.assignment_group = current.assignment_group;
    newTask.short_description = current.short_description;
    newTask.description = current.description;
    
    var newTaskSysID = newStory.insert();
    
GlideSysAttachment.copy('sn_safe_scrum_task', current.sys_id, 'sn_safe_scrum_task', newTask.sys_id);
(current, previous);

    
}

Can anyone point out where i am going wrong here? 

1 ACCEPTED SOLUTION

@Blaze Llanos - Please update the task attachment line as below. I replaced current.sys_id with tasks.getValue('sys_id')

GlideSysAttachment.copy('sn_safe_scrum_task', tasks.getValue('sys_id'), 'sn_safe_scrum_task', newTaskSysID);

 Also, for copying story attachments use below

GlideSysAttachment.copy('sn_safe_story', stories.getValue('sys_id'), 'sn_safe_story', newStorySysID);
Regards,
Muhammad

View solution in original post

14 REPLIES 14

I updated, but it am still getting the same result there should be 6 unique stories with attachments and 1 unique task for each with attachments

//New Feature
var newFeature = new GlideRecord('sn_safe_feature');
newFeature.initialize();
    newFeature.priority = current.priority;
    newFeature.sn_safe_epic = current.sn_safe_epic;
    newFeature.sn_safe_program = current.sn_safe_program;
    newFeature.priority = current.priority;
    newFeature.type = current.type; 
    newFeature.demand = current.demand; 
    newFeature.u_documentation_url = current.u_documentation_url;
    newFeature.short_description = current.short_description;
    newFeature.assignment_group = current.assignment_group;
    newFeature.description = current.description;

    var newFeatureID = newFeature.insert();
GlideSysAttachment.copy('sn_safe_feature', current.sys_id, 'sn_safe_feature', newFeature.sys_id);
(current, previous);

//Retrieve Stories
var stories = new GlideRecord('sn_safe_story');
stories.addQuery('sn_safe_feature', current.getUniqueValue());
if (current.u_copy_stories=='Not Completed') {
    stories.addQuery('state', '!=',3);
}
stories.query();
gs.info("STORIES RETURNED:{0}", stories.getRowCount());
while (stories.next()){
    var newStory = new GlideRecord('sn_safe_story');
    newStory.initialize();
    newStory.sn_safe_feature = newFeatureID;
    newStory.sn_safe_epic = stories.sn_safe_epic;
    newStory.sn_safe_program = stories.sn_safe_program;
    newStory.priority = stories.priority;
    newStory.type = stories.type; 
    newStory.demand = stories.demand; 
    newStory.u_documentation_url = stories.u_documentation_url;
    newStory.short_description = stories.short_description;
    newStory.assignment_group = stories.assignment_group;
    newStory.description = stories.description;
    
    var newStorySysID = newStory.insert();
    
GlideSysAttachment.copy('sn_safe_story', current.sys_id, 'sn_safe_story', newStorySysID);
(current, previous);

//Retrieve Tasks
var tasks = new GlideRecord('sn_safe_scrum_task');
stories.addQuery('sn_safe_story', stories.getValue("sys_id")); 
if (current.u_copy_stories=='Not Completed') {
    stories.addQuery('state', '!=',3);
}
tasks.query();
gs.info("TASKS RETURNED:{0}", tasks.getRowCount());
while (tasks.next()){
    var newTask = new GlideRecord('sn_safe_scrum_task');
    newTask.initialize();
    newTask.sn_safe_story = newStorySysID;
    newTask.priority = tasks.priority;
    newTask.type = tasks.type;
    newTask.assignment_group = tasks.assignment_group;
    newTask.short_description = tasks.short_description;
    newTask.description = tasks.description;
    
    var newTaskSysID = newTask.insert();
    
GlideSysAttachment.copy('sn_safe_scrum_task', current.sys_id, 'sn_safe_scrum_task', newTaskSysID);
(current, previous);

    
}
}

find_real_file.png

 

find_real_file.png

@Blaze Llanos - In the below addQuery please use tasks instead of stories 

tasks.addQuery('sn_safe_story', stories.getValue("sys_id")); 

if (current.u_copy_stories=='Not Completed') {
    tasks.addQuery('state', '!=',3);
}

Please make the change and do share the results of below info message.

gs.info("TASKS RETURNED:{0}", tasks.getRowCount());

Regards,
Muhammad

No tasks were created - There should have been 3 stories that had tasks each with 1 attachment.  find_real_file.png

6 Stories were created correctly but attachments did not come over. 

 

find_real_file.png

 

1 Feature was created correctly with Attachments 

For the attachment thing... Please replace current with stories 

GlideSysAttachment.copy('sn_safe_story', stories.getValue('sys_id'), 'sn_safe_story', newStorySysID);

For tasks issue can you please apply required filter on the tasks List view and share the filter here by copying it as encoded query? 

Regards,
Muhammad

I modified the story as such. then I ran the script and the log saiid that it returned.1 entry out of 6.

 

I commented out 

/*if (current.u_copy_stories=='Not Completed') {
    tasks.addQuery('state', '!=',3);
}*/

To make sure that there wasn't any logic blocking the tasks from being populated. 

 

//New Feature
var newFeature = new GlideRecord('sn_safe_feature');
newFeature.initialize();
    newFeature.priority = current.priority;
    newFeature.sn_safe_epic = current.sn_safe_epic;
    newFeature.sn_safe_program = current.sn_safe_program;
    newFeature.priority = current.priority;
    newFeature.type = current.type; 
    newFeature.demand = current.demand; 
    newFeature.u_documentation_url = current.u_documentation_url;
    newFeature.short_description = current.short_description;
    newFeature.assignment_group = current.assignment_group;
    newFeature.description = current.description;

    var newFeatureID = newFeature.insert();
GlideSysAttachment.copy('sn_safe_feature', current.sys_id, 'sn_safe_feature', newFeature.sys_id);
(current, previous);

//Retrieve Stories
var stories = new GlideRecord('sn_safe_story');
stories.addQuery('sn_safe_feature', current.getUniqueValue());
if (current.u_copy_stories=='Not Completed') {
   stories.addQuery('state', '!=',3);
}
stories.query();
gs.info("STORIES RETURNED:{0}", stories.getRowCount());
while (stories.next()){
    var newStory = new GlideRecord('sn_safe_story');
    newStory.initialize();
    newStory.sn_safe_feature = newFeatureID;
    newStory.sn_safe_epic = stories.sn_safe_epic;
    newStory.sn_safe_program = stories.sn_safe_program;
    newStory.priority = stories.priority;
    newStory.type = stories.type; 
    newStory.demand = stories.demand; 
    newStory.u_documentation_url = stories.u_documentation_url;
    newStory.short_description = stories.short_description;
    newStory.assignment_group = stories.assignment_group;
    newStory.description = stories.description;

    var newStorySysID = newStory.insert();
}
GlideSysAttachment.copy('sn_safe_story', current.sys_id, 'sn_safe_story', newStorySysID);
(current, previous);

//Retrieve Tasks
var tasks = new GlideRecord('sn_safe_scrum_task');
tasks.addQuery('sn_safe_story', stories.getValue("sys_id")); 
/*if (current.u_copy_stories=='Not Completed') {
    tasks.addQuery('state', '!=',3);
}*/

tasks.query();
gs.info("TASKS RETURNED:{0}", tasks.getRowCount());
while (tasks.next()){
    var newTask = new GlideRecord('sn_safe_scrum_task');
    newTask.initialize();
    newTask.sn_safe_story = newStorySysID;
    newTask.priority = tasks.priority;
    newTask.type = tasks.type;
    newTask.assignment_group = tasks.assignment_group;
    newTask.short_description = tasks.short_description;
    newTask.description = tasks.description;
    
    var newTaskSysID = newTask.insert();
    
GlideSysAttachment.copy('sn_safe_scrum_task', current.sys_id, 'sn_safe_scrum_task', newTaskSysID);
(current, previous);   
}

 

find_real_file.png