Jump to content
  • 0

Insert Trigger - Insert Only on First Matching Condition


kpcollier

Question

I have an insert trigger that is joining with a Estimate_Material_Table by an ID field. For all of the matching records in the Estimate_Material_Table, I need to see if any of the Cost Codes fields equal a certain value. If they do, then I need the Insert Block to proceed and create a new record. 

This is working with my current setup, however it is inserting the new record for each Estimate Material Table record that matches the condition. 

I'm trying to get it to only add once.

In Example, if the matching records from the Estimate Material Table had 3 records with Cost Code of 500, it should still only insert the new record once. Currently, it would insert 3 new records, which is not what I'm looking for.

It is almost like I need to group these matching Estimate records, see if any of the condition values exist, and then do the insert block instead of looping through each matching record. I just couldn't figure out how to do this.

Any help is appreciated.

 

matadd.thumb.PNG.c12497b4bf8266602bd4339e9f1fc300.PNG

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Hi @kpcollier not sure if this will work, but how about you add another condition under WHERE that the combination of the records being inserted does not yet exist in the table where you are inserting? I hope this makes sense. So basically it's going to be WHERE (combination of field values does not exist in table) AND (your current conditions under OR). I hope this helps or gives you an idea. :) 

Link to comment
Share on other sites

  • 0

Thanks @ParkLoey

I made a workflow where I have a Variable that has a Select statement with the same OR conditions as above, and is selecting just a random field from them as a value. This way, if any of the conditions are true, the Variable will have a value, but if none of the conditions are true, the variable will be blank. Then I used that variable in an If/Then block, where if Variable is not blank, then add the new record. If it is blank, then don't add the record. 

If this does not work or seems slow, I am definitely going to give your suggestion a try.

Link to comment
Share on other sites

  • 0

Have you tried adding TOP 1 in the SELECT block? That way only 1 matching record from the query should be inserted even if 100 records were to match the condition. Just like the smaller select statements at the bottom of your screenshot. But that would mean that it will always prioritize the first condition that it matches like 500

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...