Is it possible to restrict the number of favorites to "30" in My Favorites page in Employee center

chandanap
Tera Expert

Is it possible to restrict the number of favorites to "30" in My Favorites page in Employee center. If user try to add favorites more than "30", an error message should be displayed.

2 REPLIES 2

Likitha Patel
Tera Expert

Hi @chandanap ,

Yes, it is possible to limit the number of favorites displayed on the "My Favorites" page in the Employee Center to 30.

Here’s how you can do it:
1. Write a business rule on the Portal Favorite table.
2. Table: sp_favorite

3. When to run: before insert

4. In advance section add the below script and save it.

(function executeRule(current, previous /*null when async*/) {
    // Initialize a GlideRecord object for the 'sp_favorite' table
    var favoriteRecord = new GlideRecord("sp_favorite");
    
    // Query to find all favorite records for the current user
    favoriteRecord.addQuery("user", gs.getUserID());
    favoriteRecord.query();
    
    // Check if the user has exceeded the maximum allowed favorites
    if (favoriteRecord.getRowCount() > 30) {
        // Display an error message to the user
        gs.addErrorMessage("You have reached the maximum limit of 30 favorites. Please remove some items to add new favorites.");
		
        // Abort the transaction
        current.setAbortAction(true);
    }
})(current, previous);

5. In the employee center mark an item as a favorite. If the count is greater than 30 error message is shown as previewed in the below screenshot.

 

Likitha30_Patel_1-1734541654178.png

 

 

Uncle Rob
Kilo Patron

Why would you want to do this?