Copy UI Action

kunal16
Tera Expert

Hi,

I am trying to create a copy UI action on Project form. With the use of the UI action, the user should be able to create a copy of a project and its task.

UI Action:

Name: Project Support Services

Table: Project (pm_project)

Action name: copy_project

Onclick: createCopy()

Condition: gs.getUser().isMemberOf( ­"Project Support Services") && current.isValidRecord()

Script

function createCopy()

{

  var a1 = g_form.getUniqueValue();

  gr = new GlideRecord('pm_project');

  gr.get(a1);

  var sysid = gr.sys_id;

  var ga = new GlideAjax('cloneproject');

  ga.addParam('sysparm_name','Copyproject');

  ga.addParam('sysparm_projtask',sysid);

  ga.getXMLWait();

  var prj_sysid = ga.getAnswer();

  window.location= "/pm_project.do?sys_id="+prj_sysid;

}

Script Include:

var cloneproject = Class.create();

cloneproject.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  Copyproject: function() {

  var proj_sysid = this.getParameter('sysparm_projtask');

  var gr = new GlideRecord('pm_project');

  gr.get(proj_sysid);

  var task1 = new GlideRecord('pm_project');

  //task1.initialize();

  task1.u_financially_closed = 'false';

  task1.description = "Copy from: " + gr.number + ' - ' + current.description;

  task1.phase = 'planning';

  //gr.work_start = current.work_start;

  task1.state = '-5';

  task1.work_cost = '';

  task1.percent_complete = '0';

  task1.effort = '0';

  task1.insert();

  if (gr.hasAttachments()) {

  GlideSysAttachment.copy('pm_project', gr.sys_id, 'pm_project', task1.sys_id);

  // task1.update();

  }

  gs.addInfoMessage('New Project ' +"'"+ task1.number +"'"+ ' has been created');

  // Copy Project task

  var ptask = new GlideRecord('pm_project_task');

  ptask.addQuery('parent',gr.sys_id);

  ptask.query();

  while(ptask.next())

  {

  var fields = ptask.getFields();

  var nptask = new GlideRecord('pm_project_task');

  //Copy all fields except

  nptask.initialize();

  for (var j = 0; j < fields.size(); j++)

  {

  var ele = fields.get(j);

  if (ele.hasValue() && ele.getName() != 'number' && ele.getName() != 'opened_by' && ele.getName() != 'assigned_to' && ele.getName() != 'assignment_group' && ele.getName() != 'opened_at' && ele.getName() != 'sys_updated_by' && ele.getName() != 'time_constraint' && ele.getName() != 'start_date' && ele.getName() != 'end_date' && ele.getName() != 'u_task_capitalization_tag' && ele.getName() != 'comments')

  {

  var itm = ele.getName();

  nptask[itm] = ele;

  }

  }

  nptask.setValue('parent',gr.sys_id);

  nptask.setValue('state',-5);

  nptask.setValue('duration', 0);

  nptask.setValue('effort', 0);

  nptask.setValue('percent_complete', 0);

  nptask.setValue('work_cost', 0);

  nptask.setValue('work_effort', 0);

  nptask.insert();

  gs.addInfoMessage('New Project task ' +"'"+ nptask.number +"'"+ ' has been created');

  }

  },

  type: 'createCopy'

});

With the script include and UI action, I am unable to implement the functionality?

Any leads will be appreciated.

5 REPLIES 5

Brian Dailey1
Kilo Sage

Hi Kunal,



A few suggestions...



  1. In the beginning of your script, you don't need to lookup a record to get the sys_id to pass along to the AJAX call, you can just use:
            var sysid = g_form.getUniqueValue();
  2. If you are wanting to copy *everything* from this project as-is, I think it's much simpler just to use the following to create an exact copy of the existing record and capture the new project's sys_id:
            var new_sysid = current.insert();

    If you needed to change some values, you could then lookup the new project and alter them with an update:
            var gr = new GlideRecord('pm_project');
            gr.get(new_sysid);
            gr.work_notes = "Cloned Project";
            //(etc.)        

  3. It doesn't even have to be an AJAX situation, you can use server-side code (e.g., "current") in a UI Action if you don't select "Client" in its properties.


After cloning the project, you can still loop through the existing project record's tasks and do the same thing to clone them as well.




See if any of that helps,



-Brian