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