How to auto-populate knowledge article body with a field value

sam222
Tera Contributor

Hi guys - 

I received an inquiry from a user at my company and I am not sure if there's an ideal path. Hope you can help, and thank you in advance! 

 

Issue: Is there a way to auto-update a text in the article body based on a related field value? Let's say, I want to populate or auto-populate a part of the text in my article body with the 'Updated' value. 

As you can see below, I have a table with columns Last Review Date and manually inputted date value. However, I want to see if it can pull in the article's updated value easily?

I don't want this to be a global function. 

 

sam222_0-1704772331190.png

 

3 REPLIES 3

Iraj Shaikh
Mega Sage
Mega Sage

Hi @sam222 

In ServiceNow, if you want to auto-update a part of the text in an article body based on a related field value, you can achieve this by using a combination of scripting and dynamic content blocks if you are using the Knowledge Management Advanced plugin. Here's a general approach to how you might accomplish this:

1. Dynamic Content Blocks: If you have the Knowledge Management Advanced plugin, you can use dynamic content blocks to insert content that can be updated dynamically based on field values.

2. Scripting: You can write a script to update the article body text when the 'Updated' field value changes. This could be done using a Business Rule or a Workflow/Flow Designer flow that triggers on update of the field.

Here's a high-level overview of how you might set this up:

Using Dynamic Content Blocks:

1. Create a Dynamic Content Block: Create a dynamic content block that references the field you want to display. This block can be inserted into your knowledge articles.

2. Insert the Dynamic Content Block into the Article: Edit your knowledge article and insert the dynamic content block where you want the updated field value to appear.

3. Update the Field: When the related field value is updated, the dynamic content block should automatically reflect the new value wherever it is used.

Using Scripting:

1. Create a Business Rule:
      - When to run: Choose when the business rule should run (e.g., before or after an update).
      - Conditions: Define conditions for when the rule should execute, such as when the 'Updated' field changes.
      - Script: Write a script that updates the article body with the new value from the 'Updated' field.

2. Workflow/Flow Designer:
     - Create a Flow: Use the Flow Designer to create a flow that triggers on an update to the 'Updated' field.
     - Action: Add an action to update the article body with the new value.

Here's a simple example of what a business rule script might look like:

 

(function executeRule(current, previous /*null when async*/) {
    // Check if the 'Updated' field has changed
    if (current.updated.hasChanged()) {
        // Retrieve the knowledge article record
        var kbArticle = new GlideRecord('kb_knowledge');
        if (kbArticle.get(current.sys_id)) {
            // Update the article body with the new 'Updated' value
            var updatedValue = current.updated.getDisplayValue();
            var newBody = kbArticle.text.replace(/Last Review Date: \S+/, "Last Review Date: " + updatedValue);
            kbArticle.text = newBody;
            kbArticle.update();
        }
    }
})(current, previous);

 

Note: The above script is a simplified example and assumes that the 'Updated' field is named `updated` and that the article body field is named `text`. You will need to adjust the script to match your actual field names and the specific format of your article body content.

 

Be aware that scripting solutions can require maintenance if the structure of your articles or the fields you reference change in the future.

Please mark this response as correct or helpful if it assisted you with your question.

Community Alums
Not applicable
 
@Iraj Shaikh How do you insert the dynamic content block in the Article body?

Community Alums
Not applicable

@Iraj ShaikhHow to accomplish your 2nd suggestion: Insert the Dynamic Content Block into the Article