Typehead Search (Global search) on portal needs to search only Record Producers items

rishabh31
Mega Sage

Hi Team,

 

Please help with the customization of the Server Script for the Typehead search widget, so that it only searches Record producer's items.

 

Currently, it searches all visible KB articles, requests, etc. needs to restrict these searches & allow to search only Record Producer items.

 

Please help with this ask. 

 

Thanks

 

 

 

1 ACCEPTED SOLUTION

BharathChintala
Mega Sage

@rishabh31 you will not need script modifications for this

go to your portal page and update search source correctly. and make sure TypeHead is enabled in search source

BharathChintala_0-1678869559431.png

BharathChintala_1-1678869755194.png

 

 

If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

View solution in original post

7 REPLIES 7

Community Alums
Not applicable

Thanks @Community Alums for the URL, I followed the same & try to customize the Current Server Script for the widget- typeHead search but was unable to get the result.

 

Below I am sharing the default server Script for the Typeahead Search widget, requesting to please customize it in such a way so that it only searches RECORD PRODUCER ITEMS, kindly help.

 

(function() {
	var portalRecord = $sp.getPortalRecord();
	data.aisEnabled = $sp.isAISearchEnabled();
	data.q = $sp.getParameter('q');

	if (options.refresh_page_on_search_submission == undefined)
		data.refreshPageOnSearch = true;
	else
		data.refreshPageOnSearch = options.refresh_page_on_search_submission;

	// AI Search
	if (data.aisEnabled) {
		data.searchApplicationId = options.search_application || portalRecord.getValue('search_application');
		gs.addInfoMessage(data.searchApplicationId);
		options.placeholder = options.placeholder ? gs.getMessage(options.placeholder) : gs.getMessage('Search');
		data.aiSearchSourceFilter = "";
		var aiSearchSource = options.ai_search_source_filter;
		var filterGr = new GlideRecord('sys_search_filter');
		filterGr.addQuery('name','sc_cat_item');
		filterGr.addQuery('ais_search_source', aiSearchSource);
		filterGr.addQuery('search_context_config', data.searchApplicationId);
	//	filterGr.addQuery('search_context_config','test');
		filterGr.addActiveQuery();
		filterGr.orderBy('order');
		filterGr.query();
		if (filterGr.next())
			data.aiSearchSourceFilter = filterGr.getUniqueValue();
        var portalId = portalRecord && portalRecord.getUniqueValue();
        var resultMap = $sp.getAISearchResultsActionConfig(data.searchApplicationId, portalId);
	//	var resultMap = $sp.getAISearchResultsActionConfig('test', portalId);
		resultMap = JSON.parse(resultMap);
        data.tableToSourceMap = resultMap.tableToSourceMap;
        data.sourceToPageMap = resultMap.sourceToPageMap;
        data.urlMap = resultMap.urlMap;
		return;
	}

	if (input && input.action === "GlideSPSearchAnalyticsUpdateRank") {
		input.action = "";
		var textSearchAnalytics = $sp.publishSearchAnalytics(JSON.stringify(input.payload));
		return ;
	}
	if (options.title) {
		options.title = gs.getMessage(options.title);
	}

	data.resultMsg = gs.getMessage("Search results.");
	data.navigationMsg = gs.getMessage("To navigate, use up and down arrow keys.");
	data.portalID = $sp.getPortalRecord().getUniqueValue();
	data.searchMsg = gs.getMessage("Search");

	data.noResultsFoundMsg = gs.getMessage("No results found");

	data.isSuggestionsEnabled = gs.getProperty('glide.search.suggestions.enabled');
	data.searchTypeBehavior = gs.getProperty('glide.service_portal.search_as_you_type_behavior').toLowerCase();
	data.typeaheadWaitMS = getIntProperty('glide.service_portal.typeahead.wait_ms', '1000');
	data.typeaheadMinLength = getIntProperty('glide.service_portal.typeahead.min_length', '3');
	data.isLocationTrackerDisabled = gs.getProperty('glide.service_portal.disable_location_tracker');

	var searchSources;
	data.searchType = null;
	data.searchSources = [];
	if ($sp.getParameter("id") == "search" && $sp.getParameter("t")) {
		data.searchType = $sp.getParameter("t");
		searchSources = $sp.getSearchSources(data.portalID);
	} else {
		var contextualSearchSourceIDs = options.contextual_search_sources || null;
		searchSources = $sp.getSearchSources(data.portalID, contextualSearchSourceIDs);
		if (searchSources.length == 1) {
			data.searchType = searchSources[0].id;
		}
	}

	data.searchSourceSysIds = [];
	data.typeaheadTemplates = {};
	data.searchSourceConfiguration = {};
	searchSources.forEach(function(source) {
		data.searchSourceSysIds.push(source.sys_id);
		if (source.isTypeaheadEnabled) {
			data.searchSources.push(source.id);
		}
		var sourceTemplateConfiguration = {
			sys_id: source.sys_id,
			glyph: source.typeaheadGlyph,
			linkToPage: source.typeaheadPage
		};

		if (source.isAdvancedTypeaheadConfig) {
			sourceTemplateConfiguration.type = "ADVANCED";
			sourceTemplateConfiguration.template = "sp-typeahead-" + source.id + ".html";
			data.typeaheadTemplates["sp-typeahead-" + source.id + ".html"] = $sp.translateTemplate(source.typeaheadTemplate);
		} else {
			sourceTemplateConfiguration.type = "SIMPLE";
			if (!sourceTemplateConfiguration.linkToPage)
			console.log("Warning: No typeahead page or URL provided for search source " + source.name);
		}

		data.searchSourceConfiguration[source.id] = sourceTemplateConfiguration;
	});

	function getIntProperty(name, defaultValue) {
		var answer = parseInt(gs.getProperty(name, defaultValue));
		return isNaN(answer) ? defaultValue : answer;
	}

})();

jaheerhattiwale
Mega Sage
Mega Sage

@rishabh31 Open your portal record

And Remove "Knowledge bases" from the search source related list, keep only catalogs

jaheerhattiwale_0-1673423189036.png

In catalogs related list add the catalog which the record producers are part of and remove all others

jaheerhattiwale_1-1673423240658.png

 

If there is no common catalog for all record producers then create a new catalog and add it to all the record producers you want to see in search result, And add that catalog in catalogs related list

 

Please mark as correct answer if this solves your issue.

 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Thanks @jaheerhattiwale for the URL, I followed the same but was unable to get the result as I want to do this through script because it impacts the search of other custom portals as well.

 

Below I am sharing the default server Script for the Typeahead Search widget, requesting to please customize it in such a way that it only searches RECORD PRODUCER ITEMS, kindly help.

 

(function() {
	var portalRecord = $sp.getPortalRecord();
	data.aisEnabled = $sp.isAISearchEnabled();
	data.q = $sp.getParameter('q');

	if (options.refresh_page_on_search_submission == undefined)
		data.refreshPageOnSearch = true;
	else
		data.refreshPageOnSearch = options.refresh_page_on_search_submission;

	// AI Search
	if (data.aisEnabled) {
		data.searchApplicationId = options.search_application || portalRecord.getValue('search_application');
		gs.addInfoMessage(data.searchApplicationId);
		options.placeholder = options.placeholder ? gs.getMessage(options.placeholder) : gs.getMessage('Search');
		data.aiSearchSourceFilter = "";
		var aiSearchSource = options.ai_search_source_filter;
		var filterGr = new GlideRecord('sys_search_filter');
		filterGr.addQuery('name','sc_cat_item');
		filterGr.addQuery('ais_search_source', aiSearchSource);
		filterGr.addQuery('search_context_config', data.searchApplicationId);
	//	filterGr.addQuery('search_context_config','test');
		filterGr.addActiveQuery();
		filterGr.orderBy('order');
		filterGr.query();
		if (filterGr.next())
			data.aiSearchSourceFilter = filterGr.getUniqueValue();
        var portalId = portalRecord && portalRecord.getUniqueValue();
        var resultMap = $sp.getAISearchResultsActionConfig(data.searchApplicationId, portalId);
	//	var resultMap = $sp.getAISearchResultsActionConfig('test', portalId);
		resultMap = JSON.parse(resultMap);
        data.tableToSourceMap = resultMap.tableToSourceMap;
        data.sourceToPageMap = resultMap.sourceToPageMap;
        data.urlMap = resultMap.urlMap;
		return;
	}

	if (input && input.action === "GlideSPSearchAnalyticsUpdateRank") {
		input.action = "";
		var textSearchAnalytics = $sp.publishSearchAnalytics(JSON.stringify(input.payload));
		return ;
	}
	if (options.title) {
		options.title = gs.getMessage(options.title);
	}

	data.resultMsg = gs.getMessage("Search results.");
	data.navigationMsg = gs.getMessage("To navigate, use up and down arrow keys.");
	data.portalID = $sp.getPortalRecord().getUniqueValue();
	data.searchMsg = gs.getMessage("Search");

	data.noResultsFoundMsg = gs.getMessage("No results found");

	data.isSuggestionsEnabled = gs.getProperty('glide.search.suggestions.enabled');
	data.searchTypeBehavior = gs.getProperty('glide.service_portal.search_as_you_type_behavior').toLowerCase();
	data.typeaheadWaitMS = getIntProperty('glide.service_portal.typeahead.wait_ms', '1000');
	data.typeaheadMinLength = getIntProperty('glide.service_portal.typeahead.min_length', '3');
	data.isLocationTrackerDisabled = gs.getProperty('glide.service_portal.disable_location_tracker');

	var searchSources;
	data.searchType = null;
	data.searchSources = [];
	if ($sp.getParameter("id") == "search" && $sp.getParameter("t")) {
		data.searchType = $sp.getParameter("t");
		searchSources = $sp.getSearchSources(data.portalID);
	} else {
		var contextualSearchSourceIDs = options.contextual_search_sources || null;
		searchSources = $sp.getSearchSources(data.portalID, contextualSearchSourceIDs);
		if (searchSources.length == 1) {
			data.searchType = searchSources[0].id;
		}
	}

	data.searchSourceSysIds = [];
	data.typeaheadTemplates = {};
	data.searchSourceConfiguration = {};
	searchSources.forEach(function(source) {
		data.searchSourceSysIds.push(source.sys_id);
		if (source.isTypeaheadEnabled) {
			data.searchSources.push(source.id);
		}
		var sourceTemplateConfiguration = {
			sys_id: source.sys_id,
			glyph: source.typeaheadGlyph,
			linkToPage: source.typeaheadPage
		};

		if (source.isAdvancedTypeaheadConfig) {
			sourceTemplateConfiguration.type = "ADVANCED";
			sourceTemplateConfiguration.template = "sp-typeahead-" + source.id + ".html";
			data.typeaheadTemplates["sp-typeahead-" + source.id + ".html"] = $sp.translateTemplate(source.typeaheadTemplate);
		} else {
			sourceTemplateConfiguration.type = "SIMPLE";
			if (!sourceTemplateConfiguration.linkToPage)
			console.log("Warning: No typeahead page or URL provided for search source " + source.name);
		}

		data.searchSourceConfiguration[source.id] = sourceTemplateConfiguration;
	});

	function getIntProperty(name, defaultValue) {
		var answer = parseInt(gs.getProperty(name, defaultValue));
		return isNaN(answer) ? defaultValue : answer;
	}

})();