Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to publish multiple software models to catalogs

MercBuilding
Tera Guru

Hi,

   We are publishing software models to catalog to make it available for end user to request, BUT its getting hard to open each model and publish it. Is there any way we can publish 50+ models at once?

 

Thanks!

11 REPLIES 11

Hi @RaghavSh ,

   Even i was able to figure out the table name but when i tried to run the script the category is empty, right now the sysid of your software catalog is working fine. But iam trying to publish the software models for all the entitlements, so i want to keep this forum open to discuss the script.

Thanks!

Fadi Haddad
Tera Contributor

I've developed this script to automate publish  more than 10 software model. 

(function publishSoftwareModels() {
const CATEGORY_SYS_ID = 'ENTER YOUR SC_CATEGORY SYS ID TO ADD';
const LOG_SOURCE = 'SoftwareModelPublishing';
const TABLE = 'pc_software_cat_item';
const TYPE = 'model';

var processedCount = 0;
var errorCount = 0;

try {
var util = new ProductCatalogUtils();

var gr = new GlideRecord('cmdb_software_product_model');
gr.addEncodedQuery('ADD YOUR ENCODED QUERY LIST OF SOFTWARE MODELS TO PUBLISH);
// gr.setLimit(10); // test limit
gr.query();

var rowCount = gr.getRowCount();
if (rowCount === 0) {
gs.info(LOG_SOURCE + ': No software models found to publish for the specified criteria.');
return;
}

gs.info(LOG_SOURCE + ': Starting bulk publishing. Found ' + rowCount + ' models.');

while (gr.next()) {
var modelDisplayValue = gr.getDisplayValue();
gs.info(LOG_SOURCE + ': Processing model: ' + modelDisplayValue);

// ➜ SKIP models already linked
if (!gr.product_catalog_item.nil()) {
gs.info(LOG_SOURCE + ': Skipped (already published): ' + modelDisplayValue);
continue;
}

var newItemId = util.createProductCatalog(
gr, // GlideRecord
CATEGORY_SYS_ID, // category sys_id
TYPE, // "model"
TABLE // catalog item table
);

if (newItemId) {
gr.setValue('product_catalog_item', newItemId);
if (gr.update()) {
processedCount++;
} else {
gs.error(LOG_SOURCE + ': Failed to update model after publishing: ' + modelDisplayValue);
errorCount++;
}
} else {
gs.error(LOG_SOURCE + ': Failed to publish: ' + modelDisplayValue);
errorCount++;
}
}

gs.info(LOG_SOURCE +
': Bulk publishing complete. Successfully processed ' +
processedCount +
' models. Failed to process ' +
errorCount +
' models.'
);

} catch (e) {
gs.error(LOG_SOURCE + ': An unexpected error occurred: ' + e.message);
}
})();