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

Thanks for sharing! We actually had to back out our custom development on the publish page because it was causing update xmls for the catalog item to move to test and prod without the workflow in the xml (so the workflow dropped from the catalog items in those environments despite us being able to see the workflow in dev). I believe this was happening when someone made changes... and now I can't recall if it was changes to the model or change to the catalog item. It probably was just wonky "just learning javascript" issues (I wrote it when we first moved into servicenow and I had no background in javascript). It came up when we upgraded to Istanbul. Prior to that, the code worked without issue. Just something to keep an eye on.


We are facing same issue, we wrote async Business Rule like

ar = new GlideRecord('io_set_item');
ar.insert();
ar.variable_set = 'adf446634f040f8099c8e3518110c7cf';
ar.sc_cat_item = current.sys_id;
ar.order = 100;
ar.update();

it is working fine but not showing variable set until we refresh the form. any way to fix this?

Tammy Simmons
Tera Guru

Thanks for input from everyone on this since I also was having this issue in our new Jakarta instance. I was able to add variable sets to a published service creator service form via on after insert business without any problems. Variable was being duplicated in my variable set on publish from product catalog also and I was redirected to the new published catalog item. I did find a solution that worked for me and thought I would share. Keep in mind I tried other combinations of a business rule and the one below is the only one that worked for me. Hope this helps.

1. Created a new custom field on the 'Product Catalog Item [pc_product_cat_item]' table (i wanted 'Software Catalog Item' and 'Hardware Catalog Item' extended tables to inherit this field. This field is boolean with default false.

2. Created a new business rule:

    a. Runs on the 'Hardware Catalog [pc_hardware_cate_item]' table.

    b. On Before UPDATE

    c. Condition = 'Active is true' AND 'New Custom Field is false'.

    d. Advanced script adds the variable set to the catalog item. (See below for the script include script to add the variable set to the catalog item if needed). I also added additional code statement to this business rule to set the current record's 'New Custom Field to true'.

             current.u_newvariablename = true;

 The above worked great. I don't have any doubt that an update to the published catalog item will be needed from the group that publishes it from the model. The price defaults to $0 so there has to be an update on it.

Also created the same br on the 'Software Catalog [pc_software_cat_item]' table.

 

I realized that there may be a case where our group may create a copy of an existing hardware or software catalog item instead of publishing from the product model. So, for that scenario.

a. Created an 'on After Insert' business rule to run on both catalog tables.

b. Condition = 'New Custom Field is true' (had to go flag all current software and hardware catalog items to true that didn't have this value but we already manually added the variable set to).

c. Advanced script included the below script to add the variable set.

If anyone needs the advanced script to add the variable set, I am actually using a script include:

 This is in the br script:

         new scriptincludeName().addStandardVariableSet(current.sys_id);

Below is the code in the script include.

 

-----------------

scriptIncludeName:

var scriptIncludeName = Class.create();

scriptIncludeName .prototype = {
    initialize :function(){
    },
   

    addStandardVariableSet: function(itemGRObject) {
        var setItem = new GlideRecord('io_set_item');
        setItem.initialize();
        setItem.variable_set = 'sysid of the variable set';
        setItem.sc_cat_item = itemGRObject;
        setItem.insert();        
    },

    type: 'scriptIncludeName'
};

------------------