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

sr81
Kilo Contributor

Yes, it is possible. You must clone/duplicate the 'workflow context' as well.


Jim Coyne
Kilo Patron

Not out of box, but I created a UI Action to do just that:



Name: Copy


Table: Workflow Activity Definition [wf_activity_definition]


Order: 100


Action name: u_copy_activity_definition


Active: Checked


Show insert: NOT checked


Show update: Checked


Client: Checked


Form button: Checked


Onclick: u_copyActivityDefinitionClient()


Condition: current.canCreate()


Script:


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


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


              }


    }


}



It will copy the record and then loop through looking for the Activity Variables (and any choice list items), Condition Defaults, as well as UI Policies and UI Policy Actions.   I think it is complete.



I've attached an XML export of the UI Action record.


troyp
Giga Contributor

Jim,



This was great, but ... not quite.       UI Policies refer to the original; not the duplicate.



Here's what I'm trying to do:   I'm exploring how to make managing variables on request item tasks a bit easier.   I wanted to duplicate the Catalog Task activity, then extend it to copy the vars down from the item to the task.   That part wasn't hard to figure out.



Duplicating the Activity Definition ... is proving to be challenging.



Your UI Action helped get the bulk of the work, DONE!   Thanks!



The security rules didn't allow me to remove the 'variables' variable - but got around that.    



Now the problem is in the UI Policies.   They still all refer to the variables on the original item; not the newly created one.     I'm wondering where else there are references to the old activity definition.



Any thoughts?



Thanks,


          Troy


Hi Jim / Others



Here is a slightly (one line) adjusted script to fix the UI Policy's not running on the new wf_activity record.


Was just forcing a condition change in text, it wasnt possible from the condition list due to some complexity in the handling, so it has to be done by the copy script.


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


              }  


    }  


}