Remove duplicated children in an association
- Last Updated: March 24, 2022
- 3 minute read
- Corticon
- Version 6.3
- Documentation
Problem
For a Customer->Address association
(one-to-many), each address must be unique.
Solution
Compare every address associated with a customer with every other address associated with that customer, and -- when a match is found -- remove (or mark) one of the addresses.
The following example compares all pairs of addresses that meet a filter
condition. That process occurs in no specific order so you might notice that one run
starts with address 4 and address 2 (id=1 < id=4),
yet the next time it runs, it might start with address 3 and address 1 (id=2 < id=3), so the results might seem different.
However, all that is required is that only one of each unique
address survives.
To ensure that the filtering process is controlled, you need unique identifier
attribute values to distinguish the instances. If the address already has an attribute
that is a unique identifier, then you could use that in the filter; otherwise, you need
to create a transient, integer attribute, id, in the
Address entity in the Vocabulary:
Using the created identifier attribute, create a Rulesheet to identify each
unique address. It uses two aliases to run through the addresses associated with a given
customer. The actions initialize the id, and then add
an incremented id value to each associated Address in memory:
After each address has a unique identity, the second Rulesheet does the removal action. It iterates through the associations to identify whether an association has a match, and, if it does, to remove the matching association from memory, as shown:
A Ruleflow puts the two Rulesheets into sequence, as shown:
A Ruletest that uses this Ruleflow as the test subject shows the "survivors" in its output:
After this processing is done, subsequent Rulesheets in the Ruleflow see only unduplicated addresses for each customer.
Flagging duplicate children
You might want to identify the duplicated records rather than delete
them. To do so, just uncheck (or delete) the .remove
action, and add an appropriate .comment value to the
address. This examples uses, 'Duplicate', as shown:
When the same Ruletest runs, this time shows all the input records, with duplicated records displaying their comment values:
'Duplicate'.