Create “Favorites” Section in Knowledge Home Page

Arun Kailash
Tera Contributor

ArunKailash_0-1779359079133.png

ArunKailash_1-1779359632411.png

ArunKailash_2-1779359653359.png

 

Hi ,

I am working on a requirement in the Service Operations Workspace (SOW) Knowledge Article page.

Requirement

When a user clicks the “Test” button, the selected Knowledge Article should be added to a custom favorites table. The record should store:

  • User

  • Knowledge Article

I have already:

  • Created the required custom table

  • Configured the Data Resource in UI Builder

  • Added a Client Script for the button action

Currently, the functionality is working correctly, and the article is being added to the custom favorites table successfully.

Issue

I am trying to add validation so that:

  • A user can favorite a particular article only once

  • If the same user tries to favorite the same article again, it should not create a duplicate record

  • Instead, it should display a notification message such as:

    “This article has already been added to your favorites.”

I attempted to perform a duplicate check using a Lookup Records data resource before creating the record, but the validation is not behaving as expected in UI Builder / SOW Workspace.

Question

What is the recommended approach in UI Builder / Now Experience to:

  1. Check whether the favorite record already exists for the current user and article

  2. Prevent duplicate inserts

  3. Show a warning notification if the article is already favorited

Any guidance or best practices would be greatly appreciated.

Thanks in advance!

 

3 REPLIES 3

Tanushree Maiti
Tera Patron

Hi @Arun Kailash 

 

try with this :

 

  1. Navigate to System Definition > UI Actions  for your Knowledge table
  • Name: Test
  • Table: Knowledge [kb_knowledge]
  • Client: true
  • Form Action: Check Show updateShow insert
  • Onclick: addToFavorites()

Sample code/Not tested:

 

function addToFavorites() {

    var kb_sys_Id = g_form.getUniqueValue();

   

    var ga = new GlideAjax('KnowledgeTestFavoriteUtils');

    ga.addParam('sysparm_name', 'addFavorite');

    ga.addParam('sysparm_kb_id', kb_sys_Id);

   

    ga.getXMLAnswer(function(response) {

        if (response == 'success') {

            g_form.addInfoMessage('Knowledge Article is added to favorites.');

        } else if (response == 'exists') {

            g_form.addErrorMessage('This Knowledge Article is already in your favorites.');

        } else {

            g_form.addErrorMessage('An error has been occurred while adding to favorites.');

        }

    });

}

 

 

  1. Navigate to System Definition > Script Includes >New
  • Name: KnowledgeFavoriteUtils
  • Client callable: true

Sample Code :

 

var KnowledgeTestFavoriteUtils = Class.create();

KnowledgeTestFavoriteUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

    addFavorite: function() {

        var kbSysId = this.getParameter('sysparm_kb_id');

        var userId = gs.getUserID();

       

         var addFavGr = new GlideRecord('x_custom_favorites_table'); // Replace with your table name

        addFavGr.addQuery('user', userId);

        addFavGr.addQuery('knowledge_article', kbSysId);

        addFavGr.query();

       

        if (!addFavGr.hasNext()) {

            addFavGr.initialize();

            addFavGr.setValue('user', userId);

            addFavGr.setValue('knowledge_article', kbSysId);

            addFavGr.insert();

            return 'success';

        }

        return 'exists';

    },

    type: ' KnowledgeTestFavoriteUtils'

});

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Hi @Tanushree Maiti Thanks for your Response,

My requirement is specifically for the kb_view page in Service Operations Workspace (SOW). Since the standard UI Action approach is not very effective for this scenario in UI Builder, I initially created a Declarative Action for the requirement.

However, I was unfortunately unable to invoke the Declarative Action directly through UI Builder on the kb_view page.

As an alternative approach, I created a custom button directly in the kb_view page using UI Builder and implemented the functionality through:

  • Client Script

  • Data Resources

  • Custom favorites table

Currently, the article is successfully being added to the custom favorites table when the button is clicked.

The only remaining challenge is implementing proper duplicate validation so that:

  • the same user cannot favorite the same article multiple times

  • and instead receives a warning notification if the article is already favorited.

If anyone has implemented a similar solution in SOW / UI Builder, especially on the kb_view page, I would appreciate any recommendations or best practices.

PradeepReddyA
Tera Guru

Hi @Arun Kailash 

 

Before inserting the record itself, try adding a validation/error message in the Data Resource. Prior to creating the record, check whether the sys_id already exists for the user. The user sys_id can be fetched from the session properties in UI Builder. If a matching record already exists, display an appropriate error message instead of allowing the insert operation.

 

Hope that helps!