Need to remove image present in all the knowledge article's Article body.

Ashutosh44
Tera Contributor

Hi Everyone,

We have more than 2k knowledge articles, there is an image present in the template present in the article body for all the articles that needs to be removed.
I have written a script and I am storing the value of the Article body field (in the form of string) in a variable.

My plan is to remove the first <tr> that has that image and update the article field value for each record.

The problem is that the HTML body is coming in form of string and I am trying to replace the first <tr> using javascript string functions. But I am not able to replace it with .replace function.

 

Here's what I tried in background script so far:
1) Used GlideRecord in kb_knowledge table

2) I am trying to update only 1 article as of now, so adding addQuery with sys_id of that particular article.

3) storing the article body in a variable (in string format)

4) trying to remove all the white spaces between HTML tags using  bodyText.replace(/>\s+</g,'><');

5) trying to replace the whole HTML code between <tr></tr> with .replace function -- This is not working.

 

Here's my background script logic:

var gr = new GlideRecord('kb_knowledge');
gr.addEncodedQuery('kb_knowledge_base=sys_id of KB');
gr.addQuery('sys_id','53bc059edb7b9340fa86fb761d96197f'); //published
gr.query();
var bodyText, newText, finalText;
gs.print(gr.getRowCount());
while(gr.next()){
bodyText = gr.getValue('text');
//remove all the white spaces between HTML tags
newText = bodyText.replace(/>\s+</g,'><');
 
// removing <tr> having image
finalText = newText.replace('<tr><td style="background-color: black; padding: 5px;"><img style="align: baseline;" title="" src="sys_attachment.do sys_id=1193b32fdbfa3e0088d3dbbb5e961992" alt="" width="224" height="42" align="baseline" border="" hspace="" vspace="" /></td></tr>',' ');
 
//gr.setWorkflow(false);
//r.autoSysFields(false);
//gr.update();
}
gs.print(typeof bodyText);

gs.print(bodyText.length);
gs.print(bodyText);
gs.print('\n \n');

gs.print(newText.length);
gs.print(newText);
gs.print('\n \n');

gs.print(finalText.length);
gs.print(finalText);



 

3 REPLIES 3

bammar
Kilo Sage
Kilo Sage

Shouldnt you uncomment this - //gr.update();

 

Also not sure how crazy this would be but what if you exported xml then somehow opened that file and edited those strings. There are probably other ways as well. 

 

Also what happens if you rename the image file- would that result in image dissapearing?

This worked for me!I exported a test document and just deleted the reference to the image between the XML tags and reimported.  Now test a bulk export, edit and import.

jaheerhattiwale
Mega Sage
Mega Sage

@Ashutosh44 Tried and tested solution.

You can use below code to remove the image row.

 

var htmlString ="<table>";
htmlString += "<tr><th>test th1</th></tr>";
htmlString += "<tr><td><img src='image1.png'/></td></tr>";
htmlString += "<tr><td><img src='image2.png'/></td></tr>";
htmlString += "</table>";

gs.info("Original string: "+htmlString);

var firstIndexOfImg = htmlString.indexOf("img");

//Find start of row
var startIndexOdTr;
for(var i=firstIndexOfImg;i > 0; i--){
    if(htmlString[i-3] == "<" && htmlString[i-2] == "t" && htmlString[i-1] == "r" && htmlString[i] == ">"){
        startIndexOdTr = i-3;
        break;
    }
}

//Find end of row
var lastIndexOdTr;
for(var i=firstIndexOfImg;i < htmlString.length; i++){
    if(htmlString[i] == "<" && htmlString[i+1] == "/" && htmlString[i+2] == "t" && htmlString[i+3] == "r" && htmlString[i+4] == ">"){
        lastIndexOdTr = i+5;
        break;
    }
}

if(startIndexOdTr && lastIndexOdTr){
var imageRow = htmlString.slice(startIndexOdTr, lastIndexOdTr);

var finalString = htmlString.replace(imageRow, "");
gs.info("Final string: "+finalString);
}
Result:
jaheerhattiwale_0-1671344604345.png

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023