Replace a line text for articles

Abigail
Tera Expert

I need to replace a text from a few articles that have the same text and I need to replace it with another one.

I don't need to replace all the text just a line of text.

 

 

Any idea how can i do this using background script?

3 REPLIES 3

Vishal Birajdar
Giga Sage

Hi @Abigail 

 

Below link might help you...!!!

 

https://www.servicenow.com/community/itsm-forum/replace-string-within-knowledge-article/m-p/2338953

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hello @Vishal Birajdar 

The problem is that I need to change the Article Body, and is not working when i try to change the text.

var grKbKnowledge = new GlideRecord('kb_knowledge');
grKbKnowledge.addEncodedQuery("sys_class_name!=^textLIKE800-262-7378 (CNA-SERV)^number=KB0102647^sys_updated_onON2023-04-17@javascript:gs.dateGenerate('2023-04-17','start')@javascript:gs.dateGenerate('2023-04-17','end')");
grKbKnowledge.orderByDesc('number');
grKbKnowledge.setLimit(100);
grKbKnowledge.query();
while (grKbKnowledge.next()) {
    //grKbKnowledge.initialize();
    oldtext = grKbKnowledge.getValue('text');
    //gs.addInfoMessage('text: ' + oldtext);
    newtext=oldtext.replace("<strong> is simply dummy text of the printing and typesetting industry. </strong>random text", "<strong>is simply dummy text of the printing and typesetting industry. </strong>random text new");

    gs.addInfoMessage('text: ' + newtext);
}

Hi @Abigail 

 

Update the while loop as follows

 

while (grKbKnowledge.next()) {
      grKbKnowledge.text   = grKbKnowledge.text.replace("<strong> is simply dummy text of the printing and typesetting industry. </strong>random text", "<strong>is simply dummy text of the printing and typesetting industry. </strong>random text new");

grKbKnowledge.update();

}
 
You can remove that <strong> from replace string as it is nothing but html style tags
Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates