How can we exclude any catalog item from recommended for you widget in Servicenow

Shreyas Khade
Giga Guru

Is there a way to exclude a particular catalog item from showing up on the recommended for you widget in the employee centre portal ? 

1 ACCEPTED SOLUTION

Shreyas Khade
Giga Guru

The code pasted below should ideally serve the purpose of just removing the catalog item from appearing on the portal.

Note: sn_hr_sp.ignoredCatalogs is a system property created that stores comma separated sys_ids of all those catalog items that are to be omitted from appearing on the portal.

 

(function() {
if (input && input.action == "loadData") {
var maxDisplayCount = 10;
data.instanceId = $sp.getDisplayValue("sys_id");
data.title = gs.getMessage(options.title);
data.userPredictionCount = options.user_prediction_count;
data.cardType = (options.list_type === 'Card List') ? options.card_behaviour + '' : '';
data.listType = (options.list_type === 'Card List') ? 'card-list' : 'simple-link';
var knowledgeBases = String($sp.getKnowledgeBases());

var displayCount = options.limit > maxDisplayCount ? maxDisplayCount : options.limit;
data.viewType = (displayCount > 3) ? 'max-view' : 'min-view';
var recentActivityCutoffInDays = options.recent_activity_cut_off_in_days;
var recentActivityCutoffDate = new GlideDateTime();
recentActivityCutoffDate.addDaysLocalTime(-recentActivityCutoffInDays);
var RelevantForYouUtil = new sn_hr_sp.RelevantForYouUtil();
var taxonomyId = $sp.getTaxonomies();
var catalogPayload = {
users: null,
catalogCount: displayCount,
ignoredCatalogs: 'Other Request',
recentActivityCutoffDate: recentActivityCutoffDate,
taxonomyId: taxonomyId
};
var articlesPayload = {
users: null,
knowledgeBases: knowledgeBases,
kbRecords : $sp.getAllKBRecords(knowledgeBases),
articleCount: displayCount,
recentActivityCutoffDate: recentActivityCutoffDate,
taxonomyId: taxonomyId
};

if (options.category === 'knowledge')
data.dataItems = RelevantForYouUtil.getRelevantArticlesAndCatalogs(articlesPayload, null, displayCount, data.userPredictionCount, taxonomyId);
else if (options.category === 'catalog')
data.dataItems = RelevantForYouUtil.getRelevantArticlesAndCatalogs(null, catalogPayload, displayCount, data.userPredictionCount, taxonomyId);
else
data.dataItems = RelevantForYouUtil.getRelevantArticlesAndCatalogs(articlesPayload, catalogPayload, displayCount, data.userPredictionCount, taxonomyId);

 

for(var i=0; i<data.dataItems.length;i++) {
var catIgnore = gs.getProperty('sn_hr_sp.ignoredCatalogs').split(',');
for (var j=0; j<catIgnore.length; j++)
{
if(data.dataItems[i].sys_id == catIgnore[j] ) {
data.dataItems.splice(i,1);
}
}
}

data.widgetsData = getWidgetsData(data.dataItems, data.listType);
}

function getWidgetsData(dataResults, listType) {
listType = (listType === 'card-list') ? 'card' : 'simple_link';
var dataPayload = [];
dataResults.forEach(function(article) {
var itemPayload = {};
var widgetName = article.category == 'catalog' ? 'catalog-content' : 'kb-content';
itemPayload.widgetTitle = data.title;
itemPayload.content = article.sys_id;
itemPayload.content_table = article.category == 'catalog' ? 'sc_cat_item' : 'kb_knowledge';
itemPayload.listType = listType;
itemPayload.widgetData = $sp.getWidget(widgetName, itemPayload);
dataPayload.push(itemPayload.widgetData);
});
return dataPayload;
}
})();

 

 

 

Please Mark the answer as helpful if it works 🙂

View solution in original post

6 REPLIES 6

Amit Gujarathi
Giga Sage
Giga Sage

HI @Shreyas Khade ,
I trust you are doing great.

Here's a step-by-step guide:

  1. Identify the catalog item you want to exclude. Note down its sys_id.
  2. Go to the Employee Centre portal and navigate to "Catalog" > "Catalog Items".
  3. Open the catalog item you wish to exclude.
  4. Under the "Order Guide" related list, click on the "View" link next to the order guide that includes the catalog item.
  5. Locate the "Client Script" related list and click on "New".
  6. In the new client script record, set the "Type" field to "onLoad".
  7. Add the following code to the script box:

 

function onLoad() {
    g_form.setVisible('sc_order', false);
}

 

  • This code will hide the catalog item on the order guide form.

    1. Save the client script record.

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hello @Amit Gujarathi , Hope you are doing well

The catalog item just needs to be kept hidden from appearing in the recommended for you widget but should be visible when searched.

Shreyas Khade
Giga Guru

The code pasted below should ideally serve the purpose of just removing the catalog item from appearing on the portal.

Note: sn_hr_sp.ignoredCatalogs is a system property created that stores comma separated sys_ids of all those catalog items that are to be omitted from appearing on the portal.

 

(function() {
if (input && input.action == "loadData") {
var maxDisplayCount = 10;
data.instanceId = $sp.getDisplayValue("sys_id");
data.title = gs.getMessage(options.title);
data.userPredictionCount = options.user_prediction_count;
data.cardType = (options.list_type === 'Card List') ? options.card_behaviour + '' : '';
data.listType = (options.list_type === 'Card List') ? 'card-list' : 'simple-link';
var knowledgeBases = String($sp.getKnowledgeBases());

var displayCount = options.limit > maxDisplayCount ? maxDisplayCount : options.limit;
data.viewType = (displayCount > 3) ? 'max-view' : 'min-view';
var recentActivityCutoffInDays = options.recent_activity_cut_off_in_days;
var recentActivityCutoffDate = new GlideDateTime();
recentActivityCutoffDate.addDaysLocalTime(-recentActivityCutoffInDays);
var RelevantForYouUtil = new sn_hr_sp.RelevantForYouUtil();
var taxonomyId = $sp.getTaxonomies();
var catalogPayload = {
users: null,
catalogCount: displayCount,
ignoredCatalogs: 'Other Request',
recentActivityCutoffDate: recentActivityCutoffDate,
taxonomyId: taxonomyId
};
var articlesPayload = {
users: null,
knowledgeBases: knowledgeBases,
kbRecords : $sp.getAllKBRecords(knowledgeBases),
articleCount: displayCount,
recentActivityCutoffDate: recentActivityCutoffDate,
taxonomyId: taxonomyId
};

if (options.category === 'knowledge')
data.dataItems = RelevantForYouUtil.getRelevantArticlesAndCatalogs(articlesPayload, null, displayCount, data.userPredictionCount, taxonomyId);
else if (options.category === 'catalog')
data.dataItems = RelevantForYouUtil.getRelevantArticlesAndCatalogs(null, catalogPayload, displayCount, data.userPredictionCount, taxonomyId);
else
data.dataItems = RelevantForYouUtil.getRelevantArticlesAndCatalogs(articlesPayload, catalogPayload, displayCount, data.userPredictionCount, taxonomyId);

 

for(var i=0; i<data.dataItems.length;i++) {
var catIgnore = gs.getProperty('sn_hr_sp.ignoredCatalogs').split(',');
for (var j=0; j<catIgnore.length; j++)
{
if(data.dataItems[i].sys_id == catIgnore[j] ) {
data.dataItems.splice(i,1);
}
}
}

data.widgetsData = getWidgetsData(data.dataItems, data.listType);
}

function getWidgetsData(dataResults, listType) {
listType = (listType === 'card-list') ? 'card' : 'simple_link';
var dataPayload = [];
dataResults.forEach(function(article) {
var itemPayload = {};
var widgetName = article.category == 'catalog' ? 'catalog-content' : 'kb-content';
itemPayload.widgetTitle = data.title;
itemPayload.content = article.sys_id;
itemPayload.content_table = article.category == 'catalog' ? 'sc_cat_item' : 'kb_knowledge';
itemPayload.listType = listType;
itemPayload.widgetData = $sp.getWidget(widgetName, itemPayload);
dataPayload.push(itemPayload.widgetData);
});
return dataPayload;
}
})();

 

 

 

Please Mark the answer as helpful if it works 🙂

Hi @Shreyas Khade 

NOTE - EXCLUDE ANY CATALOG ITEM USING BOOLEAN / CHECKBOX AS PER USER CHOICE
Used the above code but slightly different by using the check box (named - EXCLUDE)  which is created  

 

var cat_Ignore = [];
    var ignore =new GlideRecord('sc_cat_item');
    ignore.addEncodedQuery('u_exclude=true');
    ignore.query();
    while(ignore.next()){
        cat_Ignore.push(ignore.getUniqueValue());
    }
    for(var i=0; i<data.dataItems.length;i++) {
    for (var j=0; j<cat_Ignore.length; j++){
     if(data.dataItems[i].sys_id == cat_Ignore[j] ) {
        data.dataItems.splice(i,1);
        }
       }
       }
Please Mark the answer as helpful if it works 😀