Copying a Variable Set

This script was designed specifically for easily creating a copy of a variable set but it could be easily adapted to create a copy of any other record (and associated records) in ServiceNow. Rather than use a field-by-field copy technique, this script works more like an ‘insert’ on the records it touches. Because of this, you get an exact copy without having to specify each and every field to copy. If you don’t want an exact copy of a particular field, you can overwrite it in the script. You can implement this copy functionality to copy a variable set by creating a UI action with the following settings…

Copy Set UI Action
Name: Copy Set
Table: Variable Set (item_option_new_set)
Action name: copy_set
Form button: True
Script:

//Make sure current changes are saved...
current.update();

//Copy the variable set
copyVS();
function copyVS() {
//Get the current sys_id value for querying
var vsID = current.sys_id.toString();
//Initialize new variable set for insertion
var newVS = current;
//Override the variable set name with a new name
newVS.name = current.name + ' (COPY)';
current.insert();

//Copy associated variables and client scripts
copyVar(vsID);
copyCS(vsID);
gs.addInfoMessage('Variable set ' + newVS.name + ' created.');
action.setRedirectURL(newVS);
}
function copyVar(vsID) {
//Copy the associated variables
var vars = new GlideRecord('item_option_new');
vars.addQuery('variable_set', vsID);
vars.query();
while(vars.next()){
//Get the current sys_id value for querying
var varID = vars.sys_id.toString();
var newVar = vars;
newVar.variable_set = current.sys_id;
vars.insert();
copyChoice(varID, newVar.sys_id.toString());
}
}
function copyCS(vsID) {
//Copy the associated catalog client scripts
var scripts = new GlideRecord('catalog_script_client');
scripts.addQuery('variable_set', vsID);
scripts.query();
while(scripts.next()){
var newCS = scripts;
newCS.variable_set = current.sys_id;
scripts.insert();
}
}
function copyChoice(varID, newVarID) {
//Copy the associated question choices for choice variables
var choices = new GlideRecord('question_choice');
choices.addQuery('question', varID);
choices.query();
while(choices.next()){
var newChoice = choices;
newChoice.question = newVarID;
choices.insert();
}
}

Date Posted:

May 19, 2010

Share This:

7 Comments

  1. Scott Stechmesser September 3, 2010 at 1:27 am

    Awesome script to use. Works great. How would you modify it to be able to copy a Catalog UI Policy?

    • Mark Stanger September 7, 2010 at 2:56 pm

      Check out the ‘Insert with Actions’ UI Action on the ‘UI Policy’ table. If it’s not in your instance you can pull the code from https://demo.service-now.com.

  2. John Vo September 20, 2016 at 12:19 pm

    Mark,

    I created this UI Action but I don’t see the button. Am I missing something?

    Thanks,
    John

    • Mark Stanger September 20, 2016 at 12:30 pm

      Standard UI action rules apply so you’ll want to make sure it’s set up as a form button and the conditions for displaying it are correct. There’s nothing special about getting this one to work.

  3. Kevin Leonard October 13, 2016 at 2:02 pm

    Thank you for the brilliance of this script! Such a time saver.

    • Mark Stanger October 13, 2016 at 2:04 pm

      You’re welcome! I’m glad you got it working!

  4. Lalo October 18, 2022 at 5:11 am

    Hey Mark,

    This is awesome, thanks so much!

Comments are closed.

Categories

Tags

Loading

Fresh Content
Direct to Your Inbox

Just add your email and hit subscribe to stay informed.