<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: KB Category Picker not working with custom table in ServiceNow AI Platform forum</title>
    <link>https://www.servicenow.com/community/servicenow-ai-platform-forum/kb-category-picker-not-working-with-custom-table/m-p/1058754#M15143</link>
    <description>&lt;P&gt;Had the same problem.&lt;BR /&gt;In UI Scripts clone "jquery_columnview" script and rename it.&lt;/P&gt;
&lt;P&gt;In cloned UI Script search for "#body_kb_categories_dialog" and change it to "#body_&amp;lt;name of your dialog&amp;gt;" - in your case change it to&amp;nbsp;&lt;STRONG&gt;#body_kb_categories_dialog_2&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;In UI Page HTML change include part to the changed name of the UI Script.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 10 Jun 2020 19:57:32 GMT</pubDate>
    <dc:creator>Sam S1</dc:creator>
    <dc:date>2020-06-10T19:57:32Z</dc:date>
    <item>
      <title>KB Category Picker not working with custom table</title>
      <link>https://www.servicenow.com/community/servicenow-ai-platform-forum/kb-category-picker-not-working-with-custom-table/m-p/1058753#M15142</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I'm having issues with setting up the KB Category Picker to work with a custom lookup table / form that I've created. The desired KB Category Picker that I want is shown below; it is the same picker that is used when creating a new knowledge article ("kb_knowledge" table). It is different from a regular list in that it allows you to drill down through the different levels of categories for the selected KB.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/107052iBCEB1DAD016D6053/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My table ("u_kb_category_to_approval_group") is extended from "dl_matcher" (lookup) and has the following three user-columns:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/107056i1D59A3ADF1B21764/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These fields all have the "u_" prefix because they are custom user fields. I want to have the same KB Category Picker for my "u_kb_category" field. The default KB Category Picker is handled by the "kb_category_reference_lookup" UI macro and "kb_categories_dialog" UI page. However, they both work with the "kb_category" and "kb_knowledge_base" fields whereas my equivalent fields include the "u_" prefix. As such, I had to create a copy of both the UI macro ("kb_category_reference_lookup_2") and the UI page ("kb_categories_dialog_2") and change them accordingly to reference my prefixed fields.&lt;STRONG&gt;&amp;nbsp;The KB Category Picker dialog opens correctly for my form except it doesn't behave correctly when I select a category and press the "Ok" button; it doesn't acknowledge the selected category so an empty value is copied to the parent form's category field.&lt;/STRONG&gt;&amp;nbsp;I've troubleshooted and I don't believe that the "columnview_select" code in "kb_categories_dialog_2" client script is running as it should. It should be setting the hidden control called "categoryId" (in the dialog window) to the selected value each time a category is selected but its not. Your help in solving this issue would be greatly appreciated. Thank you!&lt;/P&gt;
&lt;P&gt;The relevant code and configurations are shown below.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;u_kb_category field decorations and reference qualifiers:&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/107055iE318FD6C3C4F3009/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;STRONG&gt;"kb_category_reference_lookup_2" UI macro XML code:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-markup"&gt;&lt;CODE&gt;&amp;lt;?xml version="1.0" encoding="utf-8" ?&amp;gt;
  &amp;lt;j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null"&amp;gt;
    &amp;lt;g:inline template="ie_checker.xml" /&amp;gt;
    &amp;lt;g:requires name="scripts/lib/jquery_includes.js"/&amp;gt;
	&amp;lt;g2:evaluate var="jvar_table_name"&amp;gt;
		var tableName = current.getTableName();
		tableName;
	&amp;lt;/g2:evaluate&amp;gt;
	&amp;lt;g2:evaluate var="jvar_contribute_kb"&amp;gt;
		var knowledgeBases = new SNC.KnowledgeHelper().getWritableKnowledgeBases();
		var answer = [];
		while (knowledgeBases.next())
				answer.push(knowledgeBases.getValue('sys_id'));
		answer;
	&amp;lt;/g2:evaluate&amp;gt;
	&amp;lt;g2:evaluate var="jvar_timeout"&amp;gt;
		gs.minutesAgo(gs.getProperty('glide.ui.session_timeout') || 40);
	&amp;lt;/g2:evaluate&amp;gt;
	&amp;lt;g2:evaluate var="jvar_msg_lookup_picker"&amp;gt;
		gs.getMessage("Lookup using picker");		
	&amp;lt;/g2:evaluate&amp;gt;	  
&amp;lt;script&amp;gt;
	var canCreateCategory='false', oldKnowledgeBase, category_link_id;
	function getCompatibleColumnWidth() {
		if ((window.frameElement) $[AMP]$[AMP] ( window.frameElement.id == 'dialog_frame')){
			return (window.frameElement.getWidth() * 0.75);
		}

		// Resize if displayed from a dialog
		return 700; // Default value for content width
	}

	function canContribute(kbId) {
		return '$[jvar_contribute_kb]'.indexOf(kbId) &amp;gt; -1;
	}

	function openDialog() {
		var kb_knowledge_base = g_form.getValue('u_kb_knowledge_base');
		var kb_knowledge_base_gr = g_form.getReference('u_kb_knowledge_base');
		var table_name = g_form.getTableName();
		
		//Verify for canCreate access only if Knowledge Base is changed
		if(oldKnowledgeBase!=kb_knowledge_base){
			var ga = new GlideAjax('KBCategoryCrud');
			ga.addParam('sysparm_name','canCreate');
			ga.addParam('sysparm_id', kb_knowledge_base); // ID of parent Knowledge Base
			var responseXML = ga.getXMLWait();
			if (responseXML $[AMP]$[AMP] responseXML.documentElement) {
				canCreateCategory = responseXML.getElementsByTagName("result")[0].getAttribute("canCreateCategory");
				oldKnowledgeBase = kb_knowledge_base;
			}else{
				console.log("ERROR: Error in checking for canCreate access on category. Please check the server response below.");
				console.log(responseXML);
				canCreateCategory = 'false';
			}
		}
	
		if (!kb_knowledge_base || !kb_knowledge_base.length)
			return;
		if (kb_knowledge_base_gr.kb_version === '3') {
			//Allow only those KBs to which current user can contribute and are created newly by him
			if (!canContribute(kb_knowledge_base) $[AMP]$[AMP] !(kb_knowledge_base_gr.sys_created_by == g_user.userName $[AMP]$[AMP] kb_knowledge_base_gr.sys_created_on &amp;gt; '$[jvar_timeout]'))
				return ;
		}
		var disable_editing = 'false';
		if(canCreateCategory == 'false' || kb_knowledge_base_gr.disable_category_editing == 'true'){
			disable_editing = 'true';
		}
		var dialog = new GlideDialogWindow('kb_categories_dialog_2');
		dialog.setTitle('${gs.getMessage('Category picker')}');
		dialog.setSize(getCompatibleColumnWidth(), 250);
		dialog.setPreference('kb_knowledge_base', kb_knowledge_base);
		dialog.setPreference('disable_editing', disable_editing);
		dialog.hideCloseButton();
		dialog.on('beforeclose', function(){
			setTimeout(function(){
				if(category_link_id){		
					$j(category_link_id).focus();
				}});
		});
		dialog.render();
		var isQuirksMode = (document.compatMode !== 'CSS1Compat');
		if (!isQuirksMode) {
			// In modern browsers only: make the dialog box fixed to avoid it scrolling away while using its scrollable content.
			// Do dialog css adjustment only after whole dialog has been rendered
			dialog.on("bodyrendered", function() {
				var wndw = $j(window);
				var dlgSecPart = $j('.drag_section_part');
				dlgSecPart.css('width', 'auto').css('height', 'auto');
				dlgSecPart.css('position', 'fixed');
				dlgSecPart.css('top', Math.max(0, ((wndw.height() - dlgSecPart.outerHeight()) / 2) + wndw.scrollTop()) + 'px');
				dlgSecPart.css('left', Math.max(0, ((wndw.width() - dlgSecPart.outerWidth()) / 2) + wndw.scrollLeft()) + 'px');
	
			//Set focus to first element or add icon
				var firstListElm = $j("#body_kb_categories_dialog .colview-container .column .list-item");
				if(firstListElm.length)
					firstListElm.first().focus();
				else{
					$j("#body_kb_categories_dialog .colview-container .column .btn-add-cat").focus();
				}
			});
		}
	}

	$j(function($) {
		//doctype case in split layout
		var doctypeSplitView = '.vsplit.col-sm-6 &amp;gt; div';
		//doctype case in row (non split) layout
		var doctypeRowView = '.section-content &amp;gt; .row &amp;gt; .col-xs-12 &amp;gt; div';
		//non doctype case in split layout
		var nonDoctypeSplitView = '.vsplit_bottom_margin &amp;gt; tbody &amp;gt; tr';
		//non doctype case in row (non split) layout
		var nondoctypeRowView = '.wide &amp;gt; tbody &amp;gt; tr';

		var category_form_id = 'element.' + '$[jvar_table_name]' + '.u_kb_category';
		category_link_id = 'button[id="lookup.' + '$[jvar_table_name]' + '.u_kb_category"]';
		var init = function(selector) {
			$(selector).each(function() {
				if ($( this ).attr('id') == category_form_id){
					$( this ).find(category_link_id)
							.removeAttr("data-type" )
							.removeAttr("onclick" )
							.attr("type", "button")
							.click(openDialog)
							.removeAttr("title" )
							.attr("title", '$[jvar_msg_lookup_picker]' )
							.removeAttr("aria-label" )
							.attr("aria-label", '$[jvar_msg_lookup_picker]' );

				}
			});

		};
		//override of deafault onclick event will happen only for one selector that exixt on the page
		init(doctypeSplitView);
		init(doctypeRowView);
		init(nonDoctypeSplitView);
		init(nondoctypeRowView);
	});
&amp;lt;/script&amp;gt;

&amp;lt;/j:jelly&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;STRONG&gt;"kb_categories_dialog_2" UI page HTML code:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE class="language-markup"&gt;&lt;CODE&gt;&amp;lt;j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null"&amp;gt;
&amp;lt;g:requires name="scripts/lib/jquery_includes.js"/&amp;gt;
&amp;lt;g:include_script src="jquery_columnview.jsdbx"/&amp;gt;

&amp;lt;g:evaluate var="jvar_json" copyToPhase2="true"&amp;gt;
	var kb_knowledge_base = RP.getWindowProperties().get('kb_knowledge_base');
	var disable_editing = RP.getWindowProperties().get('disable_editing');
	var jsonStr = new SNC.KnowledgeHelper().getJSONCategories(kb_knowledge_base);
	
	var escape_text = gs.getProperty('glide.ui.escape_text');
	if (escape_text == 'false'){
		jsonStr = GlideStringUtil.escapeHTML(jsonStr);
	}
	jsonStr;
&amp;lt;/g:evaluate&amp;gt;
	&amp;lt;p id="err_message" class="notification notification-error" style="display:none"&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;div style="padding: 6px"&amp;gt;
	&amp;lt;div id="kbCategoriesJSON" style="display:none"&amp;gt;$[jvar_json]&amp;lt;/div&amp;gt;
	&amp;lt;input type="hidden" id="categoryId" value="" /&amp;gt;
	&amp;lt;div id="dialog_buttons" align="left"&amp;gt;
		&amp;lt;g:dialog_buttons_ok_cancel ok="setCategory()" ok_style_class="btn btn-primary category-ok-btn" ok_type="button" cancel_type="button" /&amp;gt;
	&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;!-- placeholders to read the icon char code from by name. --&amp;gt;
&amp;lt;span class="icon-edit" style="visibility:hidden; position:absolute;"/&amp;gt;
&amp;lt;span class="icon-chevron-right" style="visibility:hidden; position:absolute;"/&amp;gt;
&amp;lt;span class="icon-chevron-left" style="visibility:hidden; position:absolute;"/&amp;gt;

&amp;lt;/j:jelly&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;STRONG&gt;"kb_categories_dialog_2" UI page&amp;nbsp;client script code:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE class="language-markup"&gt;&lt;CODE&gt;var catBeingCreated = 0, //flag
	closePickerClicked = 0; //flag

// Update the form
function setCategory() {
	if(!catBeingCreated){
		var id = $j('#categoryId').val();
		g_form.setValue('u_kb_category', id);
		GlideDialogWindow.get().destroy(); //Close the dialog window
	}
	else
		closePickerClicked = 1;
}

function getCompatibleColumnWidth() {
	if (window.frameElement &amp;amp;&amp;amp; window.frameElement.id == "dialog_frame")
		return (window.frameElement.getWidth() * 0.75); // Resize if displayed from a dialog
	else
		return 700; // Default value for content width
}

$j(function() {
	var kbCategoriesJSON = $j('#kbCategoriesJSON');
	var kbCategoryId = g_form.getTableName() + '.u_kb_category';

	kbCategoriesJSON.columnview({
		jsonData: kbCategoriesJSON.html(),
		maxWidth: getCompatibleColumnWidth(),
		idValue: gel(kbCategoryId).value || false,
		blockEditing: $[disable_editing]
	}).bind({
		columnview_select: function (ev, obj) {
			alert("Here!"); // This code should run each time a row is clicked in the picker list; it behaves like that for the original picker.
			//$j("#categoryId").val(obj.id); // hidden field located at component level: gets destroyed with the component.
		},
		columnview_create: function(ev, obj) {
			// root nodes don't have a parent: use the KB article's knowledge base ID
			var kbElementVal = null;
			
			if(g_form.getControl("u_kb_knowledge_base")){
				kbElementVal = g_form.getValue("u_kb_knowledge_base");
			}else{
				return;
			}
			
			catBeingCreated = 1; //setting flag
			var id = (obj.id) ? obj.id : kbElementVal;
			var ga = new GlideAjax('KBCategoryCrud');
			ga.addParam('sysparm_name','create');
			ga.addParam('sysparm_label', obj.value);
			ga.addParam('sysparm_is_root', (obj.id ? 'false' : 'true'));
			ga.addParam('sysparm_id', id); // ID of parent
			ga.getXML(function(serverResponse) {
				if(serverResponse.status &amp;gt; 199 &amp;amp;&amp;amp; serverResponse.status &amp;lt; 300){
					var result = serverResponse.responseXML.getElementsByTagName("result");
					var id = result[0].getAttribute("sysId");
					$j("#categoryId").val(id);
					if ($j.isFunction(obj.callback)) {
						obj.callback(id, obj);
					}
					catBeingCreated = 0;
					if(closePickerClicked) // in case 'OK' button was already clicked
						setCategory(); //set the new category and then close
				}
				else{
					console.log("ERROR: Error in creating the new category. Please check the server response below.");
					console.log(serverResponse);
					catBeingCreated = 0;
					if(closePickerClicked) // in case 'OK' button was already clicked
						setCategory(); //set the new category and then close
				}
			});
		},
		columnview_update: function(ev, obj) {
			var ga = new GlideAjax('KBCategoryCrud');
			ga.addParam('sysparm_name','update');
			ga.addParam('sysparm_id', obj.id); // ID of self
			ga.addParam('sysparm_label', obj.value);
			ga.getXML(function(serverResponse) {
				var result = serverResponse.responseXML.getElementsByTagName("result");
				$j("#categoryId").val(obj.id);
				var currentId = g_form.getValue('u_kb_category');
				if (currentId === obj.id) {
					// refresh the field when the user updates the label of same category.
					g_form.setValue('u_kb_category', obj.id);
				}
				if ($j.isFunction(obj.callback)) {
					obj.callback(obj.id, obj);
				}
			});
		}
	});
});&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2020 20:33:14 GMT</pubDate>
      <guid>https://www.servicenow.com/community/servicenow-ai-platform-forum/kb-category-picker-not-working-with-custom-table/m-p/1058753#M15142</guid>
      <dc:creator>Eddie_N</dc:creator>
      <dc:date>2020-06-03T20:33:14Z</dc:date>
    </item>
    <item>
      <title>Re: KB Category Picker not working with custom table</title>
      <link>https://www.servicenow.com/community/servicenow-ai-platform-forum/kb-category-picker-not-working-with-custom-table/m-p/1058754#M15143</link>
      <description>&lt;P&gt;Had the same problem.&lt;BR /&gt;In UI Scripts clone "jquery_columnview" script and rename it.&lt;/P&gt;
&lt;P&gt;In cloned UI Script search for "#body_kb_categories_dialog" and change it to "#body_&amp;lt;name of your dialog&amp;gt;" - in your case change it to&amp;nbsp;&lt;STRONG&gt;#body_kb_categories_dialog_2&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;In UI Page HTML change include part to the changed name of the UI Script.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 19:57:32 GMT</pubDate>
      <guid>https://www.servicenow.com/community/servicenow-ai-platform-forum/kb-category-picker-not-working-with-custom-table/m-p/1058754#M15143</guid>
      <dc:creator>Sam S1</dc:creator>
      <dc:date>2020-06-10T19:57:32Z</dc:date>
    </item>
    <item>
      <title>Re: KB Category Picker not working with custom table</title>
      <link>https://www.servicenow.com/community/servicenow-ai-platform-forum/kb-category-picker-not-working-with-custom-table/m-p/1058755#M15144</link>
      <description>&lt;P&gt;Thanks so much for your help Sam, this was the missing piece!&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 13:30:12 GMT</pubDate>
      <guid>https://www.servicenow.com/community/servicenow-ai-platform-forum/kb-category-picker-not-working-with-custom-table/m-p/1058755#M15144</guid>
      <dc:creator>Eddie_N</dc:creator>
      <dc:date>2020-06-16T13:30:12Z</dc:date>
    </item>
    <item>
      <title>Re: KB Category Picker not working with custom table</title>
      <link>https://www.servicenow.com/community/servicenow-ai-platform-forum/kb-category-picker-not-working-with-custom-table/m-p/2876086#M195306</link>
      <description>&lt;P&gt;I was doing exactly the same but the dialog was not initiated at all,&lt;/P&gt;&lt;P&gt;I see no errors in the console or anything&amp;nbsp;&lt;/P&gt;&lt;P&gt;event the decorations itself seems not to be triggered or its breaking on the beginning&amp;nbsp;&lt;/P&gt;&lt;P&gt;as it should&amp;nbsp; state on the loop icon "&lt;SPAN&gt;Lookup using picker"&amp;nbsp; but it says "&lt;/SPAN&gt;&lt;SPAN&gt;Lookup using list" ans opens a standard reference list dialog ..&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;what could be the reason for that ?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 12:05:04 GMT</pubDate>
      <guid>https://www.servicenow.com/community/servicenow-ai-platform-forum/kb-category-picker-not-working-with-custom-table/m-p/2876086#M195306</guid>
      <dc:creator>Rafał Rataj</dc:creator>
      <dc:date>2024-03-25T12:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: KB Category Picker not working with custom table</title>
      <link>https://www.servicenow.com/community/servicenow-ai-platform-forum/kb-category-picker-not-working-with-custom-table/m-p/3009931#M198196</link>
      <description>&lt;P&gt;Wow was banging my head. Thanks for the solution!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Tue, 06 Aug 2024 00:55:07 GMT</pubDate>
      <guid>https://www.servicenow.com/community/servicenow-ai-platform-forum/kb-category-picker-not-working-with-custom-table/m-p/3009931#M198196</guid>
      <dc:creator>cagg</dc:creator>
      <dc:date>2024-08-06T00:55:07Z</dc:date>
    </item>
  </channel>
</rss>

