
## Mark duplicate (automations only)

This script searches a table for duplicate records and links the record in question to the duplicate found.

You need to configure a `recordId` input variable in your automation action.
This variable should refer to the ID of the record you want to search for duplication, and could come from a 'record created' trigger or a 'record enters view' trigger etc.

This script assumes your table has a 'Duplicate of' field, which is a linked record field linking to the same table.

```js

// the table to check
let table = base.getTable("Feature requests");

// the record we're searching for duplicates of.
// we need to create a 'recordId' input variable connected to a record trigger
let config = input.config();
let recordId = config.recordId;

// the field to save duplicates in. this should be a self-linked record field
let duplicatesField = table.getField("Duplicate of");

// query the table and find our record according to its id:
let query = await table.selectRecordsAsync({fields: []});
let record = query.getRecord(recordId);

// search for duplicates
let foundDuplicates = query.records.filter((potentialDuplicate) => {
    // if they're the exact same record, they're not duplicates:
    if (potentialDuplicate === record) {
        return false;
    }

    if (potentialDuplicate.name === record.name) {
        // if the names match, we've found a duplicate:
        return true;
    }

    return false;
});

console.log(`Found ${foundDuplicates.length} duplicates of ${record.name}`);

// save the duplicates:
await table.updateRecordAsync(record, {
    [duplicatesField.name]: foundDuplicates,
});

```
