Hey @ccollins I ran into this exact problem the other day and was able to write an automation using info from @Chr1sG 's post and this post from @Matt_Blais .
Automation:
- The automation triggers for new entities created from a form. I put a hidden checkbox on all of my forms to indicate this.
- The script checks a single text field (‘SKU’ in my case) to see if there is more than one copy in the DB.
- If there is, it deletes the new entry and throws an error.
There are a few downsides to this approach.
- Creating → Checking → Deleting is probably not the most efficient way to stop duplicates. I would be better to stop entity creation altogether
- The only way to indicate to the user that a duplicate has been created is to throw an error. Unfortunately if an automation errors more than 3 times it is disabled and has to be manually restarted from the automation page. This feature could help the situation.
Here is a screenshot and the code snippet:
const fibery = context.getService('fibery')
const entity_type = args.currentEntities[0].type
// Replace 'Data' with the appropriate space name
const SPACE_NAME = 'Data'
for (const entity of args.currentEntities) {
// Replace 'findMaterials' with the appropriate GraphQL command for the DB
// Replace 'sku:' and '.sku" with the appropriate text field name
const query = `{findMaterials(sku:{contains: "${entity.sku}"}){id}}`
const result = await fibery.graphql(SPACE_NAME, query)
const ids = result.data.findMaterials.length
if (ids > 1) {
await fibery.deleteEntity(entity.type, entity.id);
throw new Error("This material already exist, please check database")
}
}