The CreatorCon Call for Content is officially open! Get started here.

Automate replacing URL in links

DavidBReynolds
Mega Guru

One effect of our ServiceNow modernization project will be a new URL, which I don't have yet. While knowledge article numbers will stay the same, the URL will change.

The result is that all internal Permalink hyperlinks between articles will no longer work. 

Is there a way to use something similar to Microsoft Word's search and replace feature that can scan moved articles and replace only the URL portion of the link, leaving the rest intact?

Example:

Current hyperlink in Permalink: https://rhiprod.service-now.com.

New (guess) hyperlink: https://rhprod.servicenow.com

 

Goal: Automatically replace rhiprod.service-now with rhprod.servicenow while retaining the remainder of the link.

Thanks in advance for any suggestions.

 

Sincerely,

David Reynolds

2 REPLIES 2

Kieran Anson
Kilo Patron

Your system administrator would be able to write a script that iterates through each knowledge article body and perform a text replacement. This would likely use regex to capture multiple ways of representing a URL.

palanikumar
Giga Sage
Giga Sage

Hi,

 

Did you hard code the URLs in the Knowledge article? It is always better to use relative URL instead of this.

For example instead of storing URL as https://rhiprod.service-now.com/esc?id=kb_article&sysparm_article=KB0019522&table=kb_knowledge

you can store it as ?id=kb_article&sysparm_article=KB0019522&table=kb_knowledge which will ensure this issue will not occur in future

SInce you already have them configured that way, you can use this script to replace

var oldURL = "https://rhiprod.service-now.com";
var newURL = "https://rhprod.servicenow.com";


var grKB = new GlideRecord("kb_knowledge");
grKB.addQuery("workflow_state","published");
grKB.addEncodedQuery("textLIKEhttps://rhiprod.service-now.com");
while(grKB.nxt()){
  var htmlContent = grKB.text.toString();
  htmlContent = htmlContent.replace(matchRegEx, newURL);
  grKB.text = htmlContent;
  grKB.update()
}

 

Thank you,
Palani