Is it possible to clone a workflow activity definition with all activity variables

pkottam
Kilo Contributor

Hello,

 

I am trying to clone a workflow activity definition "Create Task" with all activity variables. I am able to clone the workflow activity Definition without any activity variables using Insert. Is there a way to clone a workflow activity definition "Create Task" with all activity variables?

17 REPLIES 17

Thanks for the update!


Missing a semicolon on line 48, but otherwise this is blankity blank fantastic!


I just tried this in Helsinki and looks like the variables layout did not copy correctly.   Which table stores that information?


denv3r
Kilo Contributor

First of all thank you guys!



But I've had a problem with related Forms, Sections. I've had different view in Workflow Editor when I choose new activity.


I've added some code to copy related Froms and Sections. As exaple I've got code from 'Copy Form' UI action in 'Form' table.


Also I've attached xml



//client function that runs 'onclick'


function u_copyActivityDefinitionClient() {


  if(confirm("Are you sure you want to make of copy of this Workflow Activity Definition?\n\nMake sure you have the proper Update Set selected.")) {


  gsftSubmit(null, g_form.getFormElement(), "u_copy_activity_definition");     //MUST call the 'Action name' set in this UI Action


  }


}




//code that runs on server


//ensure call to server-side function with no browser errors


(function() {


  if (typeof window == 'undefined') u_copyActivityDefinitionServer();


})();




function u_copyActivityDefinitionServer() {


  //server-side code goes here


  var oldId = current.getValue("sys_id");


  var oldName = current.getValue("name");


  current.name = "Copy of " + current.getValue("name");


  var newId = current.insert();


  action.setRedirectURL(current);


  gs.addInfoMessage("A copy of '" + oldName + "' was created");




  //copy the Variables


  var grVar = new GlideRecord("wf_activity_variable");


  grVar.addQuery("model", oldId);


  grVar.query();


  while (grVar.next()) {


  var oldVarTableName = grVar.getValue("name");


  grVar.model = newId;


  grVar.insert();



  //and the choice lists


  var grChoice = new GlideRecord("sys_choice");


  grChoice.addQuery("name", oldVarTableName);


  grChoice.addQuery("element", grVar.getValue("element"));


  grChoice.query();


  while (grChoice.next()) {


  grChoice.name = grVar.getValue("name");


  grChoice.insert();


  }




  }




  //copy the Condition Defaults


  var grDefault = new GlideRecord("wf_condition_default");


  grDefault.addQuery("activity_definition", oldId);


  grDefault.query();


  while (grDefault.next()) {


  grDefault.activity_definition = newId;


  grDefault.insert();


  }




  //copy the Variable UI Policies


  var grPolicy = new GlideRecord("sys_ui_policy");


  grPolicy.addQuery('table', 'wf_activity');


  grPolicy.addQuery('model_table', 'wf_activity_definition');


  grPolicy.addQuery('model_id', oldId);


  grPolicy.query();


  while (grPolicy.next()) {


  var oldPolicy = grPolicy.getValue("sys_id");


  grPolicy.model_id = newId;


  // Fix UI Policy Incorrect Conditional Match


  grPolicy.conditions = grPolicy.conditions.replace(oldId, newId);


  // End Fix


  grPolicy.insert();


  var grActions = new GlideRecord('sys_ui_policy_action');


  grActions.addQuery("ui_policy", oldPolicy);


  grActions.query();


  while (grActions.next()) {


  grActions.ui_policy = grPolicy.getValue("sys_id");


  grActions.field = grActions.getValue("field").replace(oldId, newId);


  grActions.insert();


              }



  }



  //copy Forms and Sections


  var of_name = 'var__m_' + oldId;


  var nf_name = 'var__m_' + newId;


  var sysUiForm = GlideRecord("sys_ui_form");


  sysUiForm.addQuery('name', of_name);


  sysUiForm.query();


  while (sysUiForm.next()) {


  var scm2m = new GlideRecord("sys_ui_form_section");


  scm2m.addQuery("sys_ui_form", sysUiForm.sys_id);


  scm2m.query();


  sysUiForm.name = nf_name;


  sysUiForm.sys_name = nf_name;


  var nf_id = sysUiForm.insert();


  while (scm2m.next()) {


  scm2m.sys_ui_form = nf_id;


  var nsc_id = copySection(scm2m.sys_ui_section, nf_name);


  if (!nsc_id)


  continue;




  scm2m.sys_ui_section = nsc_id;


  scm2m.insert();


  }


  }



  function copySection(id, name) {


  var sc = new GlideRecord("sys_ui_section");


  sc.get(id);


  var el = new GlideRecord("sys_ui_element");


  el.addQuery("sys_ui_section", sc.sys_id.toString());


  sc.name = name;


  var nsc_id = sc.insert();


  el.query();


  while (el.next()) {


  el.sys_ui_section = nsc_id;


  el.insert();


  }




  return nsc_id;


  }



}


Thank you and Adam Nock for this beautiful piece of code!