Creating an Event Handler based on whether or not a parameter exists in the database already

Hello,

I am trying to create a form which updates a client list database, updating and creating new users. Both of my Event Handlers work - when I press the Submit button, a new record is added, or an existing record is updated.

The problem is that these two event handlers are tied to the same "Submit" button. When I update a user, the Submit button throws an error for my "Add User" script, stating that the ClientID (parameter I am filtering by) already exists.

How can I set up an Event Handler "Only run when" rule so that the Update script runs when the ClientID already exists in the database? Alternatively, I could run the Add script when the ClientID does not exist in the database.

Thanks in advance!

I assume an upsert action doesn't work for your use case? Can you elaborate on what the differences are between the update and add scripts?

Hello,

Using the build in "Bulk Upsert" wasn't working for my case, so I just wrote up two quick SQL commands to take the parameters from the form, assign them to columns in the table manually, and insert them.

Add script just adds a line to the table, Update script updates an entry on the table, both based on a Client ID parameter.

What I'm trying to accomplish is some kind of "If, Else" statement, where "If [Client ID] Exists in the table, run "Add script,' ELSE run "Update script""

Have you considered an upsert query? This example is from Stack Exchange and applies to SQL Server 2005. I just offer this as a concept or starting point for you to consider.

IF NOT EXISTS (SELECT * FROM dbo.Employee WHERE ID = @SomeID)

    INSERT INTO dbo.Employee(Col1, ..., ColN)
    VALUES(Val1, .., ValN)

ELSE

    UPDATE dbo.Employee
    SET Col1 = Val1, Col2 = Val2, ...., ColN = ValN
    WHERE ID = @SomeID

Later versions of SQL Server include MERGE which is what we use for upserts. I don't know about other DBMS but searching for MERGE equivalents may help.

If you're set on the "Only run when" option, you will just need a comparison that will return true or false. Such as the .some() method.

yourQuery.data.ID.some(id => id === formField.value)