|
| 1 | +//set some new default values |
| 2 | +var setName = current.title; |
| 3 | +current.title = 'Copy of ' + setName; |
| 4 | +current.sys_scope = gs.getCurrentApplicationId(); |
| 5 | +current.sys_policy = ""; |
| 6 | + |
| 7 | +//insert a copy of the variable set |
| 8 | +var oldid = current.sys_id.toString(); |
| 9 | +var newid = current.insert(); |
| 10 | + |
| 11 | +if (newid) { |
| 12 | + var allVars = {}; |
| 13 | + createVariables(oldid, newid); |
| 14 | + createCatalogClientScript(oldid, newid); |
| 15 | + createCatalogUiPolicy(oldid, newid); |
| 16 | +} |
| 17 | + |
| 18 | +//creates a copy of the variables and associates them to the new variable set |
| 19 | +function createVariables(oldid, newid) { |
| 20 | + var vars = new GlideRecord('item_option_new'); |
| 21 | + vars.addQuery('variable_set', oldid); |
| 22 | + vars.addActiveQuery(); |
| 23 | + vars.query(); |
| 24 | + while (vars.next()) { |
| 25 | + var varoldid = vars.sys_id.toString(); |
| 26 | + vars.variable_set = newid; |
| 27 | + var varnewid = vars.insert(); |
| 28 | + allVars['IO:' + varoldid] = 'IO:' + varnewid.toString(); |
| 29 | + |
| 30 | + var qc = new GlideRecord('question_choice'); |
| 31 | + qc.addQuery('question', varoldid); |
| 32 | + qc.query(); |
| 33 | + while (qc.next()) { |
| 34 | + qc.question = varnewid; |
| 35 | + qc.insert(); |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +//creates a copy of the client scripts and associates to the variable set. |
| 41 | +function createCatalogClientScript(oldid, newid) { |
| 42 | + var ccs = new GlideRecord('catalog_script_client'); |
| 43 | + ccs.addQuery('variable_set', oldid); |
| 44 | + ccs.addActiveQuery(); |
| 45 | + ccs.query(); |
| 46 | + while (ccs.next()) { |
| 47 | + ccs.variable_set = newid; |
| 48 | + ccs.insert(); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +//creates a copy of the UI Policies and associates them to the new variable set |
| 53 | +function createCatalogUiPolicy(oldid, newid) { |
| 54 | + var cup = new GlideRecord('catalog_ui_policy'); |
| 55 | + cup.addQuery('variable_set', oldid); |
| 56 | + cup.addActiveQuery(); |
| 57 | + cup.query(); |
| 58 | + while (cup.next()) { |
| 59 | + var uipoldid = cup.sys_id.toString(); |
| 60 | + cup.variable_set = newid; |
| 61 | + var newuip = cup.insert(); |
| 62 | + |
| 63 | + var cupa = new GlideRecord('catalog_ui_policy_action'); |
| 64 | + cupa.addQuery('ui_policy', uipoldid); |
| 65 | + cupa.query(); |
| 66 | + while (cupa.next()) { |
| 67 | + cupa.ui_policy = newuip; |
| 68 | + cupa.variable_set = newid; |
| 69 | + var cv = cupa.catalog_variable; |
| 70 | + cupa.catalog_variable = allVars[cv]; |
| 71 | + cupa.insert(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +//Return the user to the new variable set record |
| 77 | +action.setRedirectURL(current); |
0 commit comments