logo

NJP

Copying a Variable Set with variables, client scripts, and UI Policies

Import · Feb 05, 2012 · article

I recently had a situation where I was copying lots of variable sets, and the "Insert and Stay" UI Action was getting used all too frequently. So I decided to create a "Copy" UI Action to make a copy of the variable set. Of course after I completed the script I discovered http://www.servicenowguru.com/scripting/copying-variable-set which did almost everything I wanted to do. Of course that would have saved me some time, but... oh well. The difference between mine and the servicenowguru site is that mine includes the ability to copy over all UI Policies and their actions. If this version can help others, here it is.

Overview:

This "Copy" UI Action will
* Make a copy of the variable set record
* Copy the variables

* Copy any variable questions to the new variable
* Copy all client scripts
* Copy all UI Policies

* Copy all UI Policy Actions and associated the variables to their newly created variables (which was the tricky part in all of this).

Implementation:

Create a new UI PolicySet the table to be "Variable Set (item_option_new_set)"Give it a condition. I gave it gs.hasRole('admin') Check the "Form Button" box. Or however you want the UI Action to work.Add this code:

//set some new default valuesvar name = current.name;current.name = 'Copy of ' + name;//insert a copy of the variable setvar oldid = current.sys_id.toString();var newid = current.insert();if (newid) {var allVars = {};createVariables(oldid,newid);createCatalogClientScript(oldid,newid);createCatalogUiPolicy(oldid,newid);}//creates a copy of the variables and associates them to the new variable setfunction createVariables(oldid,newid){var vars = new GlideRecord('item_option_new');vars.addQuery('variable_set', oldid);vars.query();while(vars.next()){var varoldid = vars.sys_id.toString();vars.variable_set = newid;var varnewid = vars.insert();allVars['IO:' + varoldid] = 'IO:' + varnewid.toString();var qc = new GlideRecord('question_choice');qc.addQuery('question', varoldid);qc.query();while(qc.next()){qc.question = varnewid;qc.insert();}}}//creates a copy of the client scripts and associates to the variable set.function createCatalogClientScript(oldid,newid){var ccs = new GlideRecord('catalog_script_client');ccs.addQuery('variable_set',oldid);ccs.query();while (ccs.next()) {ccs.variable_set = newid;ccs.insert();}}//creates a copy of the UI Policies and associates them to the new variable setfunction createCatalogUiPolicy(oldid,newid){var cup = new GlideRecord('catalog_ui_policy');cup.addQuery('variable_set',oldid);cup.query();while (cup.next()) {var uipoldid = cup.sys_id.toString();cup.variable_set = newid;var newuip = cup.insert();var cupa = new GlideRecord('catalog_ui_policy_action');cupa.addQuery('ui_policy', uipoldid);cupa.query();while(cupa.next()){cupa.ui_policy = newuip;cupa.variable_set = newid;var cv = cupa.catalog_variable;cupa.catalog_variable = allVars[cv];cupa.insert();}}}//Return the user to the new variable set recordaction.setRedirectURL(current);

Enjoy.var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-22945975-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();

View original source

https://www.servicenow.com/community/in-other-news/copying-a-variable-set-with-variables-client-scripts-and-ui/ba-p/2272105