Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to Avoid Duplicate entries (user's entry) into the watchlist field by using business rule

Venkat39
Tera Contributor

I have created a Before Update BR , in this i script i have achieved adding users to the watchlist when the watchlist is empty and when the watchlist is having some existing values ( , separated).

Note: I want to avoid duplicate user's entry into the watchlist. How can i achieve this? 

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var watchers = gs.getProperty('test.user.watch');
    if (current.watch_list.nil()) {
        current.watch_list = watchers;
    } else if (!current.watch_list.nil()) {

        current.watch_list += ',' + watchers;

    }
})(current, previous);
1 ACCEPTED SOLUTION

HIROSHI SATOH
Mega Sage
I haven't tested it, but how about something like this?
 
(function executeRule(current, previous /*null when async*/) {
  // Retrieve the list of watchers from the property
  var watchers = gs.getProperty('test.user.watch');

  // If the current watch_list is empty, set it to the property value
  if (current.watch_list.nil()) {
    current.watch_list = watchers;
  } else {
  // Convert the current watch_list value to an array and split it by commas
    var currentWatchers = current.watch_list.toString().split(',');

  // Convert the property value to an array by splitting it by commas
  var newWatchers = watchers.toString().split(',');

  // Add new watchers to the list if they are not already present
  newWatchers.forEach(function(watcher) {
  if (currentWatchers.indexOf(watcher) === -1) {
    currentWatchers.push(watcher);
  }
});
  // Set the updated watch_list value with no duplicates
  current.watch_list = currentWatchers.join(',');
  }
})(current, previous);
 

Comments Translation

  1. Retrieve the list of watchers from the property

    • Retrieves the watcher list from the system property.
  2. If the current watch_list is empty, set it to the property value

    • Checks if the current watch_list is empty and sets it to the value from the property if so.
  3. Convert the current watch_list value to an array and split it by commas

    • Splits the existing watch_list value into an array by commas for processing.
  4. Convert the property value to an array by splitting it by commas

    • Splits the property value into an array by commas.
  5. Add new watchers to the list if they are not already present

    • Adds each new watcher to the list if it is not already included.
  6. Set the updated watch_list value with no duplicates

    • Joins the array back into a comma-separated string and sets it as the new watch_list, ensuring no duplicates.

 

 

View solution in original post

2 REPLIES 2

HIROSHI SATOH
Mega Sage
I haven't tested it, but how about something like this?
 
(function executeRule(current, previous /*null when async*/) {
  // Retrieve the list of watchers from the property
  var watchers = gs.getProperty('test.user.watch');

  // If the current watch_list is empty, set it to the property value
  if (current.watch_list.nil()) {
    current.watch_list = watchers;
  } else {
  // Convert the current watch_list value to an array and split it by commas
    var currentWatchers = current.watch_list.toString().split(',');

  // Convert the property value to an array by splitting it by commas
  var newWatchers = watchers.toString().split(',');

  // Add new watchers to the list if they are not already present
  newWatchers.forEach(function(watcher) {
  if (currentWatchers.indexOf(watcher) === -1) {
    currentWatchers.push(watcher);
  }
});
  // Set the updated watch_list value with no duplicates
  current.watch_list = currentWatchers.join(',');
  }
})(current, previous);
 

Comments Translation

  1. Retrieve the list of watchers from the property

    • Retrieves the watcher list from the system property.
  2. If the current watch_list is empty, set it to the property value

    • Checks if the current watch_list is empty and sets it to the value from the property if so.
  3. Convert the current watch_list value to an array and split it by commas

    • Splits the existing watch_list value into an array by commas for processing.
  4. Convert the property value to an array by splitting it by commas

    • Splits the property value into an array by commas.
  5. Add new watchers to the list if they are not already present

    • Adds each new watcher to the list if it is not already included.
  6. Set the updated watch_list value with no duplicates

    • Joins the array back into a comma-separated string and sets it as the new watch_list, ensuring no duplicates.

 

 

Thanks for the quick response, let me test this form my end and get back to you.