kevinanderson
Giga Guru

We sometimes have issues where a catalog item just doesn't go into production correctly.

Whatever the issue is, the end result is we have to go back to the dev system, and recapture the missing or broken components.   For fairly complex catalog items, this could mean having to capture a large portion of variables, client scripts, and UI policies to ensure the catalog item is properly deployed.   This is a time consuming and tedious process.

I created a script, that will rip an entire catalog item and all its associated components into the current active update set.   It saves time, and guarantees the form is properly captured.

The script will force an update of each record captured to prevent update set errors (time stamp collisions) on the target system.

I borrowed very heavily from this SNGuru page.

// push a catalog item and all its components to the active update set

// in test mode, no records will be saved to update, only script output of matching records

var test_mode = true;

var catalog_item_sys_id = '{add sys_id here}'; // my catalog item

var captureVariables = function(p_sys_id, p_variable_set, p_test_mode) {

      if (typeof p_test_mode !== 'boolean') {

              p_test_mode = false;

      }

      if (typeof p_variable_set !== 'boolean') {

              p_variable_set = false;

      }

      if (JSUtil.notNil(p_sys_id)) {

              var item = new GlideRecord('item_option_new');

              if (p_variable_set) {

                      item.addQuery('variable_set', p_sys_id);

              } else {

                      item.addQuery('cat_item', p_sys_id);

              }

              item.query();

              while (item.next()) {

                      gs.print('add variable question to update set: ' + item.name)

                      if (!p_test_mode) {

                              item.setForceUpdate(true);

                              item.update();

                      }

                      // Query for question choices for variables

                      var qc = new GlideRecord('question_choice');

                      qc.addQuery('question', item.sys_id.toString());

                      qc.query();

                      while (qc.next()) {

                              //Add the variable question choice to the update set

                              gs.print('     add variable question choice to update set: ' + qc.text)

                              if (!p_test_mode) {

                                      qc.setForceUpdate(true);

                                      qc.update();

                              }

                      }

              }

      }

};

var captureUIPolicies = function(p_sys_id, p_variable_set, p_test_mode) {

      if (typeof p_test_mode !== 'boolean') {

              p_test_mode = false;

      }

      if (typeof p_variable_set !== 'boolean') {

              p_variable_set = false;

      }

      if (JSUtil.notNil(p_sys_id)) {

              var catpol = new GlideRecord('catalog_ui_policy');

              if (p_variable_set) {

                      catpol.addQuery('variable_set', p_sys_id);

              } else {

                      catpol.addQuery('catalog_item', p_sys_id);

              }

              catpol.query();

              while (catpol.next()) {

                      gs.print('add UI Policy to update set: ' + catpol.short_description)

                      if (!p_test_mode) {

                              catpol.setForceUpdate(true);

                              catpol.update();

                      }

                      //Query for ui policy actions

                      var uipact = new GlideRecord('catalog_ui_policy_action');

                      uipact.addQuery('ui_policy', catpol.sys_id.toString());

                      uipact.query();

                      while (uipact.next()) {

                              gs.print('     add UI Policy Action to update set: ' + uipact.variable + '-' + uipact.ui_policy.short_description)

                              if (!p_test_mode) {

                                      uipact.setForceUpdate(true);

                                      uipact.update();

                              }

                      }

              }

      }

};

var captureCatalogClientScripts = function(p_sys_id, p_variable_set, p_test_mode) {

      if (typeof p_test_mode !== 'boolean') {

              p_test_mode = false;

      }

      if (typeof p_variable_set !== 'boolean') {

              p_variable_set = false;

      }

      if (JSUtil.notNil(p_sys_id)) {

              var catclntscr = new GlideRecord('catalog_script_client');

              if (p_variable_set) {

                      catclntscr.addQuery('variable_set', p_sys_id);

              } else {

                      catclntscr.addQuery('cat_item', p_sys_id);

              }

              catclntscr.query();

              while (catclntscr.next()) {

                      gs.print('add catalog client scripts to update set: ' + catclntscr.name);

                      if (!p_test_mode) {

                              catclntscr.setForceUpdate(true);

                              catclntscr.update();

                      }

              }

      }

};

// 1. capture catalog item record

var gr1 = new GlideRecord('sc_cat_item');

if (gr1.get(catalog_item_sys_id)) {

      gs.print('add catalog item to update set: ' + gr1.name)

      if (!test_mode) {

              gr1.setForceUpdate(true);

              gr1.update();

      }

      captureVariables(catalog_item_sys_id, false, test_mode);

      captureUIPolicies(catalog_item_sys_id, false, test_mode);

      captureCatalogClientScripts(catalog_item_sys_id, false, test_mode);

      //5. capture related variable sets

      //Query for variable set relationships

      var vsrel = new GlideRecord('io_set_item');

      vsrel.addQuery('sc_cat_item', catalog_item_sys_id);

      vsrel.query();

      while (vsrel.next()) {

              gs.print('add variable set relationship to update set: ' + vsrel.variable_set.name);

              if (!test_mode) {

                      vsrel.setForceUpdate(true);

                      vsrel.update();

              }

              // capture variable set

              var vs = vsrel.variable_set.getRefRecord();

              if (JSUtil.notNil(vs)) {

                      //vs.sys_id);

                      captureVariables(vs.sys_id, true, test_mode);

                      captureUIPolicies(vs.sys_id, true, test_mode);

                      captureCatalogClientScripts(vs.sys_id, true, test_mode);

              }

      }

}

2 Comments