In before insert trigger I want to check if the record is already present and skip creation of a record if so.
We use external backend system which sometimes fails and sends duplicate records (nobody wants fixing it).
In before insert trigger I tried to delete
reference to a record, didn't work. AddError
also doesn't work as we want to keep process bulkified and automated.
Is there a simple way to do this?
As an idea is after creation of records run scheduler which will find duplicates and remove the last one created. But it's not an easy way :_
Best Answer
The appropriate want to do it would be to use the addError method:
This is the bulkified way of doing it. What you may be missing is the integration, code, external systems need to set the allOrNone headers or database option, etc to false. This will allow records without an error to be inserted and return the results along with any errors to the external system.
Once the above is done, you will need to evaluate your code and the external integrations to identify and fix those that may not be setting the allOrNothing to false
If you can, use external ID's when sending data from an external system and use upsert. That will handle it without the trigger code. But it will depend on your external system and if you have any control of it or not.