Export a record and its related lists

joseeduardo
Giga Expert

Hey Guys, I've googling for a while now, I still dont find a solution to export a record with related lists as well, I have researched and tested   solutions but none of them work jthe way I want! what I want is to build an UI action that exports the current record and all of its related lists into and xml file that I can import into other instances, I know there is this script include: exportWithRelatedLists but I have no Idea of how to use it!

*NOTE: Something it is very worth of highlight is that i am working on scoped applications!

Does anyone of you have any idea?

2 REPLIES 2

ITSMgal
ServiceNow Employee
ServiceNow Employee

Below is the script include code that is used to do just that in demands and projects. Hopefully the content here will give you some direction on how to do it.


This was written by a SN developer to help us with migrating projects and demands from one system to another.



This is called from a UI action



2016-11-02_12-23-26.png


var exportData = Class.create();


exportData.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  // To Export demand and its related data


  createDemandUpdateSet:function(){



  var demandlist = this.getParameter('sysparm_list');



  //current update set


  var cur_setID = gs.getPreference('sys_update_set');



  //gr maintains unload update set


  var gr = new GlideRecord('sys_update_set');


  gr.initialize();


  gr.name = 'exported_demand_data';


  gr.application = 'global';


  gr.insert();



  //change current update set to unload updateset


  new GlideUpdateSet().set(gr.sys_id);



  // forcing the data to unload update set


  var demand = demandlist.split(',');


  var len = demand.length;


  var um = new GlideUpdateManager2();


  for(i = 0; i < len; i++){


  var gr_demand = new GlideRecord('dmn_demand');


  gr_demand.get(demand[i]);


  um.saveRecord(gr_demand);


  this.addRplan(demand[i],um);


  this.addDreq(demand[i],um);


  this.addDecision(demand[i],um);


  this.addAssement(demand[i],um);


  this.addActivity(demand[i],um);


  this.addLivefeed(gr_demand.short_description,um);



  var proid = gr_demand.getValue('project');


  if(proid){


  var gr_pro = new GlideRecord('pm_project');


  gr_pro.get(proid);


  um.initialize();


  um.saveRecord(gr_pro);


  this.addProjectRelatedData(proid,gr_pro,um);


  this.addSubprojects(proid,um);


  }



  um.initialize();



  }



  new GlideUpdateSet().set(cur_setID);



  return this.downloadXML(gr);


  },



  //To export project and its related data


  createProjectUpdateSet:function(){



  var prolist = this.getParameter('sysparm_list');



  //current update set


  var cur_setID = gs.getPreference('sys_update_set');



  //gr maintains unload update set


  var gr = new GlideRecord('sys_update_set');


  gr.initialize();


  gr.name = 'exported_project_data';


  gr.application = 'global';


  gr.insert();



  //change current update set to unload updateset


  new GlideUpdateSet().set(gr.sys_id);



  // forcing the data to unload update set


  var project = prolist.split(',');


  var len = project.length;


  var um = new GlideUpdateManager2();



  for(i = 0; i < len; i++){



  var gr_pro = new GlideRecord('pm_project');


  gr_pro.get(project[i]);


  um.saveRecord(gr_pro);


  this.addProjectRelatedData(project[i],gr_pro,um);


  this.addSubprojects(project[i],um);


  um.initialize();


  }



  new GlideUpdateSet().set(cur_setID);



  return this.downloadXML(gr);


  },



  //adding sub projects


  addSubprojects:function(proid,um){


  var gr = new GlideRecord('pm_project');


  gr.addQuery('parent',proid);


  gr.query();


  while(gr.next()){


  um.initialize();


  um.saveRecord(gr);


  this.addProjectRelatedData(gr.sys_id,gr,um);


  }


  },



  //adding project related data


  addProjectRelatedData:function(proid,cur_pro,um){


  this.addProjTask(proid,um);


  this.addProjTaskRel(proid,um);


  this.addBaseline(proid,um);


  this.addTimecard(proid,um);


  this.addTasktime(proid,um);


  this.addRplan(proid,um);


  this.addIsuue(proid,um);


  this.addRisk(proid,um);


  this.addDecision(proid,um);


        this.addStories(proid,um);


  this.addActivity(proid,um);


  this.addLivefeed(cur_pro.short_description,um);


  },



  // Project Related Data


  //adding rm_story data to update set


  addStories:function(proid,um){


  var gr_story = new GlideRecord('rm_story');


  gr_story.addQuery('project',proid);


  gr_story.query();



  while(gr_story.next()){


  um.initialize();


  um.saveRecord(gr_story);


  }


  return;


  },



  //adding pm_project_task data to update set


  addProjTask:function(proid,um){


  var gr_pro_task = new GlideRecord('pm_project_task');


  gr_pro_task.addQuery('top_task',proid);


  gr_pro_task.query();



  while(gr_pro_task.next()){


  um.initialize();


  um.saveRecord(gr_pro_task);


  this.addRplan(gr_pro_task.sys_id,um);



  }


  return;


  },



  //adding task_rel_task data to update set


  addProjTaskRel:function(proid,um){


  var gr_task_rel = new GlideRecord('task_rel_task');


  gr_task_rel.addEncodedQuery('parent='+proid+'^ORchild='+proid);


  gr_task_rel.query();


  while(gr_task_rel.next()){


  um.initialize();


  um.saveRecord(gr_task_rel);


  }


  return;


  },



  //adding planned_task_baseline and planned_task_baseline_item data to update set


  addBaseline:function(proid,um){


  var gr_baseline = new GlideRecord('planned_task_baseline');


  gr_baseline.addQuery('top_task',proid);


  gr_baseline.query();



  while(gr_baseline.next()){


  um.initialize();


  um.saveRecord(gr_baseline);


  var gr_bl_item = new GlideRecord('planned_task_baseline_item');


  gr_bl_item.addQuery('baseline',gr_baseline.sys_id);


  gr_bl_item.query();


  while(gr_bl_item.next())


  {


  um.initialize();


  um.saveRecord(gr_bl_item);


  }


  }


  return;


  },



  //add time_card data to update set


  addTimecard: function(proid,um){


  var gr_timecard = new GlideRecord('time_card');


  gr_timecard.addQuery('task',proid);


  gr_timecard.query();


  while(gr_timecard.next()){


  um.initialize();


  um.saveRecord(gr_timecard);


  }


  return;


  },



  //add task_time_worked data to update set


  addTasktime: function(proid,um){


  var gr_tasktime = new GlideRecord('task_time_worked');


  gr_tasktime.addQuery('task',proid);


  gr_tasktime.query();


  while(gr_tasktime.next()){


  um.initialize();


  um.saveRecord(gr_tasktime);


  }


  return;


  },



  //add resource_plan data to update set ( common function to project and demand)


  addRplan: function(proid,um){


  var gr_rplan = new GlideRecord('resource_plan');


  gr_rplan.addQuery('task',proid);


  gr_rplan.query();


  while(gr_rplan.next()){


  um.initialize();


  um.saveRecord(gr_rplan);


  this.addRallocation(gr_rplan.sys_id,um);


  }


  return;


  },



  //add resource_allocation data to update set ( common function to project and demand)


  addRallocation: function(proid,um){


  var gr_ralloc = new GlideRecord('resource_allocation');


  gr_ralloc.addQuery('resource_plan',proid);


  gr_ralloc.query();


  while(gr_ralloc.next()){


  um.initialize();


  um.saveRecord(gr_ralloc);


  }


  return;


  },





  //add issue data to update set


  addIsuue: function(proid,um){


  var gr_issue = new GlideRecord('issue');


  gr_issue.addQuery('parent',proid);


  gr_issue.query();


  while(gr_issue.next()){


  um.initialize();


  um.saveRecord(gr_issue);


  }


  return;


  },



  //add risk data to update set


  addRisk: function(proid,um){


  var gr_risk = new GlideRecord('risk');


  gr_risk.addQuery('task',proid);


  gr_risk.query();


  while(gr_risk.next()){


  um.initialize();


  um.saveRecord(gr_risk);


  }


  return;


  },



  //add dmn_decision data to update set (common to project and demand)


  addDecision: function(proid,um){


  var gr_decision = new GlideRecord('dmn_decision');


  gr_decision.addQuery('parent',proid);


  gr_decision.query();


  while(gr_decision.next()){


  um.initialize();


  um.saveRecord(gr_decision);


  this.addPltask(gr_decision.sys_id,um);


  }


  return;


  },



  //add planned_task data to update set


  addPltask: function(proid,um){


  var gr_pltask = new GlideRecord('planned_task');


  gr_pltask.addQuery('top_task',proid);


  gr_pltask.query();


  while(gr_pltask.next()){


  um.initialize();


  um.saveRecord(gr_pltask);


  }


  return;


  },



  //add activity log data to update set (common to project and demand)


  addActivity: function(proid,um){


  var gr2 = new GlideRecord('sys_history_set');


  gr2.addQuery('id', proid);


  gr2.query();


  while(gr2.next())


  {


  um.initialize();


  um.saveRecord(gr2);


  }



  var gr = new GlideRecord('sys_history_line');


  gr.addQuery('set.id',proid);


  gr.query();



  while(gr.next()){


  um.initialize();


  um.saveRecord(gr);


  }


  return;


  },



  //add live feed data to update set (common to project and demand)


  addLivefeed: function(proid,um){


  var gr = new GlideRecord('live_message');


  gr.addQuery('group.name',proid);


  gr.query();



  while(gr.next()){


  um.initialize();


  um.saveRecord(gr);


  }


  return;


  },



  //Demand Related Data



  //add dmn_requirement data to update set


  addDreq: function(demid,um){


  var gr_req = new GlideRecord('dmn_requirement');


  gr_req.addQuery('parent',demid);


  gr_req.query();


  while(gr_req.next()){


  um.initialize();


  um.saveRecord(gr_req);


  }


  return;


  },



  //add asmt_category_result data to update set


  addAssement: function(demid,um){


  var gr_asr = new GlideRecord('asmt_category_result');


  gr_asr.addQuery('source_id',demid);


  gr_asr.query();


  while(gr_asr.next()){


  um.initialize();


  um.saveRecord(gr_asr);


  }


  return;


  },



  //download the xml file


  downloadXML:function(gr){


  var retrievedUpdateSet = new GlideRecord('sys_remote_update_set');


  retrievedUpdateSet.initialize();



  retrievedUpdateSet.description = gr.description;


  retrievedUpdateSet.name = gr.name;


  retrievedUpdateSet.release_date = gr.release_date;


  retrievedUpdateSet.remote_sys_id = gr.sys_id;


  retrievedUpdateSet.application = gr.application;



  var scopeGr = new GlideRecord('sys_scope');


  scopeGr.get(gr.application);


  if (scopeGr.isValid()) {


  retrievedUpdateSet.application_name = scopeGr.name;


  retrievedUpdateSet.application_scope = scopeGr.scope;


  retrievedUpdateSet.application_version = scopeGr.version;


  }




  retrievedUpdateSet.state = "loaded";



  var sysid = retrievedUpdateSet.insert();



  var update = new GlideRecord('sys_update_xml');


  update.addQuery('update_set', gr.sys_id);


  update.query();



  while(update.next()) {


  update.remote_update_set = retrievedUpdateSet.sys_id;


  update.update_set = '';


  update.insert();


  }


  gs.flushMessages();



  return sysid+','+gr.sys_id;


  },



  //delete unload update set from instance


  deleteUpdateset:function(){


  var sysid = this.getParameter('sysparm_sysid');


  var gr = new GlideRecord('sys_update_set');


  gr.addQuery('sys_id',sysid);


  gr.query();


  gr.next();


  gr.deleteRecord();


  return;


  },



  type: 'exportData'


});


Do you have the code for the show_export_progress UI page?