Hi everyone,
I have a list in OutSystems where each element contains the following attributes: ID, Name, Gender, and Age.
I want to validate the list to ensure that no two elements have the same combination of Name and Gender — if such duplicates exist (where Name and Gender are the same, regardless of ID or Age), I want to trigger an error.
What’s the best way to implement this logic in OutSystems?
Thanks in advance!
Hi @ZHOU YIKAI,I have implemented as per your request. I hope it helps
This doesn't work, the ListFilter will give you an error, because it re-iterates over the already iterated PersionList. Also, even if this would work, ListFilter is not the right tool for the job, since you're not really interested in the resulting list: ListAny would be better (but in this case it still doesn't work per the above!).
Hi Zhou,
Can you please give more input about this list. What are you trying to achieve ? Importing data directly in the DB or are you using this list just for display? What is the trigger for validation?
Hi @ZHOU YIKAI, here is my suggestion, create a local list variable, keep only two attributes in the list, name and gender, then assign data to this new list using you main list, so it will only have name and gender from the main list, then use 'ListDistinct' action from the system module with the new list as source and check if the length of the new list and length of the output distinct list is same or not, if it is not same that means there are duplicate values.
Note: In case you are going to make a separate action for checking the duplicate value like in the screenshot below, you can directly take the local list as input no need to get the complete main list and then assign to the local list separately.
ThanksGitansh Anand
Unfortunately, there's no easy way to do this. To check for each element whether there's a duplicate element, you need to loop over the input list twice: once for each element to check, then once to see if that element appears somewhere else. You can't use two For Eaches for this, or a For Each and one of the List actions, as you can only iterate over a list once a time. So your problem calls for an ad-hoc iteration, that is, iterate one of the loops yourself by using indexing. Alternatively, if the list isn't too big, you could make a duplicate of the list, and loop over that.
@ZHOU YIKAI : If you're comfortable with JavaScript, I've attached a working script that you can use. This script was generated with AI assistance and has been tested successfully.
I’ve created an Employee structure with the following attributes: Id, Name, Gender, and Age. You can design a screen to collect employee data and populate it into an Employee List.
When the user clicks "Check for Duplicates", the list is serialized and passed to the JavaScript function. The script loops through the employee list and uses a Map to keep track of previously processed entries. It identifies duplicates based on a combination of Name and Gender (case-insensitive), and returns a list of duplicates if any are found.