Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

@Blaze Llanos - I've updated the script. Please try below:

//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.getValue('sys_id'), 'sn_safe_feature', newFeatureID);

//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', stories.getValue('sys_id'), 'sn_safe_story', newStorySysID);

    //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); 
    }
}

I hope it will do the job but just in case after running the script please let me know what's running and what's not.

Regards,
Muhammad

This is about 99% there. 

Only thing not populating now is the attachments from the stories and the tasks. 

Everything else is working as it should 

@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

Vamsi Sreenivas
Tera Guru

Hi @Blaze Llanos , you are trying to set fields under story as same i.e., using current object so you will have all 5 stories with same values. You should use stories object instead as below:

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();

 

Mark my answer as HELPFUL / CORRECT if this help resolve your issue.

 

Regards,

Vamsi S

This helped a lot.