How to store the Knowledge article number and then prepend the entire active url before that

Shruti45
Tera Contributor

I have a client requirement to add a URL field type in catalog variables. This URL field has link to Knowledge Articles ( it changes when other field 'Job role' changes). There is an onChange Script that works to pull up the KA link( currently saved in a table with value like: /kb_view.do?sysparm_article=KB0010001)

Initially, we used part of URL so that it dynamically change when instance is different (eg: /kb_view.do?sysparm_article=KB0010001). But URL field type throws Error in RITM, SCTASK page -' invalid URL' 

find_real_file.png

I assume, URL field need full URL, with instance name.

is there a way we can store the Knowledge article number and then prepend the entire active url before that. 

example: https://[instance name].service-now.com/kb_view.do?sysparm_article=KB0010001

 

1 ACCEPTED SOLUTION

Hi Shruti,

I was thinking about some easier solution.

Would it make sense to run a script (either background or better a fix script) to actually change the link in the table with KB links from relative link to a full link with your instance prefix? In that case we would not have to touch the existing logic at all.

Something like that.

var instanceURL = gs.getProperty('glide.servlet.uri');
// need to shorter the URL by last character as it already ends with backslash, e.g. https://dev1234.service-now.com/
var instanceShort = instanceURL.slice(0,instanceURL.length - 1);

var roleMappingGR = new GlideRecord('u_job_role_mapping');

// we only want records with NOT EMPTY KB article URL and not those that start with http
var grQuery = 'u_knowledge_article_url!=NULL^u_knowledge_article_urlNOT LIKEhttp';

roleMappingGR.setLimit(1);
roleMappingGR.addEncodedQuery(grQuery);
roleMappingGR.query();

while (roleMappingGR.next()) {    
    var originalLink = roleMappingGR.getValue('u_knowledge_article_url');        
    var newLink = instanceShort + originalLink;
    
    roleMappingGR.u_knowledge_article_url = newLink;    
    roleMappingGR.update();    
}

You can test this script as it contains setLimit to 1, if all seems ok you may increase the limit or remove it completely.

 

View solution in original post

9 REPLIES 9

Right now URL are store in table, with Field type URL

is is loaded in Catalog form using OnChange Client Script

find_real_file.png

it is stored in the table like this:

find_real_file.png

 

 It is good to know we can use getProperty call to get the instance name. Please suggest how should I append this instance name with the part URL as well

Ok, it seems the script you pasted is not ideal.

You should be using GlideAjax calls instead of using GlideRecord in client script, are you familiar with GA?

In GA you would also be able to easily query the sys_property.

If not I can have a look and adjusting your script tomorrow.

Jan,

I would really appreciate if you would be able to help me with the Client side, server side scripts.

Hi Shruti,

I was thinking about some easier solution.

Would it make sense to run a script (either background or better a fix script) to actually change the link in the table with KB links from relative link to a full link with your instance prefix? In that case we would not have to touch the existing logic at all.

Something like that.

var instanceURL = gs.getProperty('glide.servlet.uri');
// need to shorter the URL by last character as it already ends with backslash, e.g. https://dev1234.service-now.com/
var instanceShort = instanceURL.slice(0,instanceURL.length - 1);

var roleMappingGR = new GlideRecord('u_job_role_mapping');

// we only want records with NOT EMPTY KB article URL and not those that start with http
var grQuery = 'u_knowledge_article_url!=NULL^u_knowledge_article_urlNOT LIKEhttp';

roleMappingGR.setLimit(1);
roleMappingGR.addEncodedQuery(grQuery);
roleMappingGR.query();

while (roleMappingGR.next()) {    
    var originalLink = roleMappingGR.getValue('u_knowledge_article_url');        
    var newLink = instanceShort + originalLink;
    
    roleMappingGR.u_knowledge_article_url = newLink;    
    roleMappingGR.update();    
}

You can test this script as it contains setLimit to 1, if all seems ok you may increase the limit or remove it completely.

 

Raj_Esh
Kilo Sage
Kilo Sage

Hi Shruti,

 

Making a Clickable Link Field as a Variable – ServiceNow Might help you.

 

Thanks,

Raj

--Raj