I have a similar situation - this script performs a search on existing records to match first and last name, and either establishes a new record, or presents the existing one.. triggered from a 'NEW' button on the layout
My dilemma seems to be that I have started to enter a new record and I have a script that fires when I exit the Customer email field
The script then searches to see if that email address exists.
The problem is that the script seems to save the new record at this point and so it either finds the record I've just entered or finds the existing record but ALSO saves the new record so now I have duplicates, grrrrr
Set up your layout so that this email address is entered into a field with global storage. Set up a relationship that matches this global field to the email address field in a table occurrence of your table.
A script trigger on your global field can use the relationship to count matching records to see if the email address has been entered before. If so, it can find that customer record. If not, it can create a new customer record and set the email address field to the value of this global email field.
If you need a more detailed description, use Post a new Answer to let me know.
How about setting the validation for the field to unique, then trapping for error 504, and presenting the user with an appropriate message if the error is thrown?