Replace string within knowledge article on all places

Rushi2
Tera Expert
Hi All, 
 
I am trying to replace one string in the knowledge article using the below script, it's not working.
 
Can anyone help me here 
 
var count = 0;
var ArticleGR = new GlideRecord('kb_knowledge');

ArticleGR.setLimit(100);
ArticleGR.setWorkflow(false);
ArticleGR.autoSysFields(false);
ArticleGR.query();
while (ArticleGR.next()) {
    var CanReadList = ArticleGR.u_kb_support_details;
    CanReadList = CanReadList.replace(/Assignment Group/g, 'Assignment');
    ArticleGR.u_kb_support_details = CanReadList;
    ArticleGR.update();

}
gs.log("KB replace count" + count);
 
Thanks,
Rushi
5 REPLIES 5

SwarnadeepNandy
Mega Sage

Hello @Rushi2,

 

Your script has some errors and limitations that prevent it from working properly. Here are some suggestions to improve your script:

  • Your script is using the replace method incorrectly. The replace method takes two parameters: a regular expression or a string that matches the pattern to be replaced, and a string that replaces the matched pattern. You are passing a regular expression /Assignment Group/g as the first parameter, but you are not using any special characters or flags that make it a valid regular expression. For example, you are not using the i flag to make it case-insensitive, or the g flag to make it global. You can either use a simple string “Assignment Group” as the first parameter, or use a proper regular expression /Assignment Group/ig as the first parameter .
  • Your script is not incrementing the count variable that you are using to log the number of replacements. You can use the ++ operator to increase the count by one for each iteration of the loop.
  • Your script is not checking if the u_kb_support_details field has a value before trying to replace it. You can use an if statement to check if the field is not null or empty before calling the replace method.

Based on these suggestions, here is an improved version of your script:

 

// Initialize a count variable
var count = 0;

// Get a GlideRecord object for the kb_knowledge table
var ArticleGR = new GlideRecord('kb_knowledge');

// Set a limit for the number of records to process
ArticleGR.setLimit(100);

// Disable workflow and auto system fields for performance
ArticleGR.setWorkflow(false);
ArticleGR.autoSysFields(false);

// Query the table
ArticleGR.query();

// Loop through the records
while (ArticleGR.next()) {
  // Get the value of the u_kb_support_details field
  var CanReadList = ArticleGR.u_kb_support_details;

  // Check if the field has a value
  if (CanReadList) {
    // Replace "Assignment Group" with "Assignment" in the field value
    CanReadList = CanReadList.replace("Assignment Group", "Assignment");

    // Set the updated value to the field
    ArticleGR.u_kb_support_details = CanReadList;

    // Update the record
    ArticleGR.update();

    // Increment the count by one
    count++;
  }
}

// Log the number of replacements
gs.log("KB replace count: " + count);

 

 

Hope this helps.

 

Kind Regards,

Swarnadeep Nandy

Hi Swarnadeep, 

 

It's not replacing. 

 

Thanks, 

Rushi

Hi @Rushi2,
Can you please share screenshots?

What kind of field is 

u_kb_support_details

Thanks

That is HTML