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

Jan Cernocky
Tera Guru

Hi Shruti,

what type is your URL variable? Seems to be String with RegEx check set for URL.

Try to change the type to URL, seems to be working just fine:

find_real_file.png

 

find_real_file.png

it is URL type

Hello Jan,

it is of 'URL' type. 

find_real_file.png

Ok I got it, it seems I might have messed around with this in the past already 🙂

Well, if you are getting the KB number from another table (I guess by AJAX call), you can construct the first part of URL by simply reading the glide.servlet.uri property (don't be confused if you don't find it in the properties list, but it works).

 

var instanceURL = gs.getProperty('glide.servlet.uri');