Add variable sets when publishing to catalog

kristenankeny
Tera Guru

We would like our asset management team to be able to use the publish to catalog links in the product catalog, but need to have them provide a workflow and then automatically add a standard variable set to the catalog item when created. I have successfully updated the UI Page and the Script Include to capture the workflow and pass that to the produced catalog item, but have not had success with adding the variables.

This is what works to add the workflow:

ProductCatalogUtils:

createProductCatalog: function(source, category, type, table, workflow) {

  var destination = new GlideRecord(table);

  this._syncRows(destination, source, category, type, workflow);

  var sys_id = destination.insert();

  this._syncPicture(source, table, sys_id);

  return sys_id

  },

publish_to_product_catalog (edited line only):

                  gr.product_catalog_item = (new ProductCatalogUtils()).createProductCatalog(gr, sc_category, type, catalog, wf_workflow);

I've added a couple new functions to the ProductCatalogUtils. A copy of the createProductCatalog which includes a call to another new function to add a variable set to the catalog item, and then from the publish_to_product_catalog, I have tried calling this new function. When I click the "Publish" link, it prompts me for the category and workflow, but when I click OK, the page refreshes and I see the prompt on the full screen. These are the new functions.

createProductCatalogwVar: function(source, category, type, table, workflow) {

  var destination = new GlideRecord(table);

  this._syncRows(destination, source, category, type, workflow);

  var sys_id = destination.insert();

  this._syncPicture(source, table, sys_id);

  this._addVariables(sys_id);

  return sys_id

  },

_addVariables: function(cat_sys) {

  var destination = new GlideRecord(io_set_item);

  destination.sc_cat_item = cat_sys;

  destination.variable_set = '37ce635a4fe9160079a5e9628110c764';

  destination.insert();

  },

I'm not sure what I'm doing incorrectly here.

Thanks,

Kristen

1 ACCEPTED SOLUTION

We were unable to resolve this in our environment with either solution above, so we will be manually adding variable sets when publishing to the catalog.


View solution in original post

12 REPLIES 12

kristenankeny
Tera Guru

I found an issue in the _addVariables script (missing quote around table for the GlideRecord):


_addVariables: function('cat_sys') {


  var destination = new GlideRecord(io_set_item);


  destination.sc_cat_item = cat_sys;


  destination.variable_set = '37ce635a4fe9160079a5e9628110c764';


  destination.insert();


  },


With this change, now when I click OK on the UI page, I get a Record not found error message.


kristenankeny
Tera Guru

The current _addVariables function is:


_addVariables: function(cat_sys) {


  var path = new GlideRecord('io_set_item');


  //destination.initialize();


  path.sc_cat_item = cat_sys;


  gs.log('The path sc cat was set to ' + cat_sys,'ProductCatalogUtils addVariables');


  path.variable_set = '95cee75a4fe9160079a5e9628110c793';


  gs.log('The path variable was set to ' + path.variable_set,'ProductCatalogUtils addVariables');


  //destination.sys_class_name = 'Catalog Variable Set';


  path.insert();


  return;


  },



We are now getting an error message that there is already # variable(s) with the same name associated with the variable set. When we open the variable set, we see one of the variables (phone, which is a single line variable) is getting duplicated each time we create the variable set - catalog item relationship. Is this a bug?


Kristen, IMHO it is a bug. I had the same issue and the only process which I found that successfully worked around the issue was to create a workflow which added the variable set(s) and call the workflow from a business rule when a new item was published to the product catalog. I opened an incident with ServiceNow and while the tech was helpful and tried several approaches it came down to 'this is custom scripting and requires professional services support'.



Here's the detail of my implementation - hope this helps. This has worked in four instances, three Fugi and one Geneva (which all exhibited the behavior you've noted). I cannot answer why this works, but it did when all other attempts failed.



1. Created a business rule to run after inserts to the Product Catalog Item table with an advanced action script to kickoff the workflow. The script is:



function onAfter(current, previous) {


    //This function will be automatically called when this rule is processed.


    var wf = new Workflow();


    var vars = {};


    var newWorkFlow = new Workflow().getWorkflowFromName('Attach Variable Sets to Product Catalog Item');


    var context = wf.startFlow(newWorkFlow, current, current.operation, vars);


}



2. Created a workflow named 'Attach Variable Sets to Product Catalog Item'. The workflow has a single 'Run Script' activity with the script below. Lines two and three contain the names of the variable sets I needed to attach.



var varVariableSet = new GlideRecord('item_option_new_set');


var grOR =   varVariableSet.addQuery('name', 'Workstation Services Fields');


grOR.addOrCondition('name', 'Common ReqUser_Date_Jstfctn');


varVariableSet.query();


while (varVariableSet.next()) {


    gs.warn("Processing variable set " + varVariableSet.name);


    // check to see if it's already present


    var chkVariableSet = new GlideRecord('io_set_item');


    chkVariableSet.addQuery('sc_cat_item', current.sys_id);


    chkVariableSet.addQuery('variable_set', varVariableSet.sys_id);


    chkVariableSet.query();


    if (chkVariableSet.getRowCount() > 0) {


          gs.warn("VariableSet " + varVariableSet.name + " already attached to item");


    }


    else {


          var addVariableSet = new GlideRecord('io_set_item');


          addVariableSet.variable_set = varVariableSet.sys_id;


          addVariableSet.sc_cat_item = current.sys_id;


          addVariableSet.insert();


    }


}


Hi Bill,



I've gotten a chance to try out this solution, but unfortunately I'm seeing the exact same behavior. I am not only scripting the insert into io_set_item, so I'm at a loss as to why variables within the set are duplicating. This is quite frustrating. Thank you for the suggestion!