Change KB article link generated by attach button

sarahmustakim
Kilo Contributor

Hi All,

If an Agent wants to add an KB article as a comment to an Incident, he can use the attach button at the "related (contextual) search results" section.

The "Attach" button creates a comment like this:

[code]<a title='Are Copyrighted Files Illegal to Have On My

Computer?' href='kb_view.do?sys_kb_id=1addc9f1474321009db4b5b08b9a7120' >KB0000009 : Are Copyrighted Files Illegal to Have On MyComputer?</a>[/code]

The url leads to the plain article without the frames.

I need to edit it to include the instance name like below:

https://<Instance Name>.service-now.com/nav_to.do?uri=/kb_view.do%3Fsys_kb_id%3D1addc9f1474321009db4b5b08b9a7120

Please assist.

Thanks,

Sarah

1 ACCEPTED SOLUTION

Gaurav Bajaj
Kilo Sage

HI Sarah,



There are two ways to achieve it but both comes with pros and cons.



1) Write a before update BR which check for the content going in the additional comments and modify it as per your need.


You can apply checks like the value contains kb_view.do and then make changes in the value before it gets saved.



This is a short and precise method which can act as a workaround.



2) The second one is that you update the   OOB script include (cxs_Knowledge) which creates this link.


     


find_real_file.png


You need to add the nav_to.do?uri= which will give your desired outcome.



Although to achieve this, you will have to remove the protection policy of the script include as its set to read-only and it won't let you edit anything.


Please run the below script in the background.



var gr = new GlideRecord("sys_metadata");  


gr.get("42ec6aa0ef62210066fc36caa5c0fbce");   // update the sys_id of script include cxs_Knowledge.


gs.print(gr.sys_policy);  


gr.sys_policy="";  


gs.print(gr.sys_policy);  


gr.update();



Disclaimer: This feature will come at expense of updating the OOB script include which will prevent it to get future updates, hence use if carefully if you want to manage the upgrades manually.




Thanks


Gaurav


View solution in original post

5 REPLIES 5

Gaurav Bajaj
Kilo Sage

HI Sarah,



There are two ways to achieve it but both comes with pros and cons.



1) Write a before update BR which check for the content going in the additional comments and modify it as per your need.


You can apply checks like the value contains kb_view.do and then make changes in the value before it gets saved.



This is a short and precise method which can act as a workaround.



2) The second one is that you update the   OOB script include (cxs_Knowledge) which creates this link.


     


find_real_file.png


You need to add the nav_to.do?uri= which will give your desired outcome.



Although to achieve this, you will have to remove the protection policy of the script include as its set to read-only and it won't let you edit anything.


Please run the below script in the background.



var gr = new GlideRecord("sys_metadata");  


gr.get("42ec6aa0ef62210066fc36caa5c0fbce");   // update the sys_id of script include cxs_Knowledge.


gs.print(gr.sys_policy);  


gr.sys_policy="";  


gs.print(gr.sys_policy);  


gr.update();



Disclaimer: This feature will come at expense of updating the OOB script include which will prevent it to get future updates, hence use if carefully if you want to manage the upgrades manually.




Thanks


Gaurav


Hi Gurav,

Can you share the BR as I would prefer not to remove the protection policy on the script include.

Best regards,

Søren

JayGervais
Kilo Sage

Hi @sarahmustakim

I don't know if you or anyone else would find this helpful, but o expand upon @Gaurav Bajaj's answer, I managed to create a solution using the client script method.

I did not want to alter or change permissions on the cxs_Knowledge script and I wanted to limit the URL changes to specific departments. Our instance is composed of multiple applications, so I also needed to include these changes in the URL for attaching related articles.

During a change event, the code looks for the string 'kb_view' from the comments field. It then takes the text between the [code] blocks and stripts it to provide the article number. I use this to create queries to access knowledge article values and the knowledge base of the article. The variables extracted are then used to generate a new URL.

Below is the client script I wrote on the 'Incident' table:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
  }

  if(newValue.includes('kb_view')){

		// gets form value from Additional comments fields
		var comment = g_form.getValue('comments');

		// retrieve KB link between [code] blocks
		var commentLink = comment.substring(
			comment.lastIndexOf('[code]') + 6,
			comment.lastIndexOf('[/code]')
		);

		// retrieve KB number from URL
		var kbNumberSub = commentLink.match(' >(.*) : ')[1];
		var kbNumber = trim(kbNumberSub);

		// query to retrieve KB article data
		var gr = new GlideRecord('kb_knowledge');
		gr.addQuery('number', kbNumber);
		gr.orderByDesc('sys_created_on');
		gr.setLimit(1);
		gr.query();
		gr.next();

		// KB Number
		var KBNum = gr.getValue('number');
		// KB Title
		var KBTitle = gr.getValue('short_description');
		// KB Knowledge Base
		var KBKnowledgeBase = gr.getValue('kb_knowledge_base');

		// query to retrieve portal data
		var url = new GlideRecord('sp_portal');
		url.addQuery('kb_knowledge_base', KBKnowledgeBase);
		url.orderByDesc('sys_created_on');
		url.setLimit(1);
		url.query();
		url.next();

		// portal URL suffix
		var portal = url.getValue('url_suffix');

		// replace Additional comments fields with new URL
		g_form.setValue("comments", newValue.replace(comment, "[code]<a title='"+KBNum+": "+KBTitle+"' href='"+portal+"?id=kb_article&sysparm_article="+KBNum+"'>"+KBNum+": "+KBTitle+"</a>[/code]"));

  }
}

Regards,

Jay

Thanks Jay, this worked great. I have been looking for another way to do this since business rule sends to links.