Knack Multi-Field Uniqueness with Integromat

A question asked often in the Knack forums and elsewhere is how to prevent a user from booking onto the same course twice - or in the case of the example we are going to use (because I had the database handy), to stop a duplicate record associating a User and a Customer Project.


This is functionality which Knack does not yet have natively so here we are going to use a combination of some Javascript (which you can easily copy and adapt) and a simple Integromat Scenario to do the actual work. This is much simpler than trying to write this in Javascript alone and works pretty well.


The idea here is that we only want to have the system allow people to create one record in the database with the same User AND the same Project. The Object is set up like this:


And the screen for entering a new {User Project} record is this:


So, in this post, I aim to give you the technique for calling Integromat when the user updates the Company Project field and the basics of the Integromat scenario which checks for duplicates. The key here is that we need to do this validation before the record is submitted and so I have worked out how to trigger this validation when the value in the Company Project field is changed.


As this is a relatively advanced topic, I will not spend too much time explaining how to create the scenario - just remember that if you use a Webhook trigger you need to call it from your code in order for it to work out the data structure it's receiving - before you can set up the rest of the scenario. The Code (Javascript)


As we need to call Integromat it follows that you will normally write the JS Code before setting up the Scenario (although you will need the web hook address to put into the code) - so here is my suggestion for the code:


//this code cross validates two fields and prevents the creation of duplicate records (in this case //a user connected to the same project twice. It uses an Interomat scenario to actually check //for the duplicate record and the response to the webhook is either Pass or Fail. //As well as turning the spinner on and off, the code also dissables the Submit button while the //validation is taking place to prevent the user from submitting the record before it is finished. $(document).on('knack-view-render.view_332', function (event, view, data) { $('#view_332-field_417').change(function() { $("#kn-loading-spinner").show(); $(".kn-submit button").attr('disabled', 'disabled'); var params = 'userID=' + $('#view_332-field_416').val() + '&projectID=' + $('#view_332-field_417').val(); commandURL = "https://hook.integromat.com/aaannndddmmmaaammm?" + params; $.get(commandURL, function(data, status){ if (data == "Fail") { alert('Please Choolse a different project! Your selection would have created a duplicate record!'); $('#view_332-field_417').val(null); $('#view_332-field_417').trigger("liszt:updated"); } $("#kn-loading-spinner").hide(); $(".kn-submit button").removeAttr('disabled'); }); }); });



The idea here is that you should be able to pretty much cut and paste this JS code into your app's code window, change the view id, field ids and web hook address and you're good to go!


The Integromat Scenario


It turns out that this is actually quite simple - a webhook trigger, search module and web hook response:



Let's look at how they are configured...


In the initial Webhook module, create a new Webhook with a suitable name:




To start, this will need you to trigger it from your code so that it can work out what you are sending to it.



Notice that the module allows you to select the values from the web hook using the parameter names you used in the Javascript.


Finally, the response - we want to send back the word 'Pass' if there are no duplicate records and 'Fail' if there are:




The way I am doing this is looking to see if any "Bundles" are returned by the search module (which indicates a duplicate is found) and then send the appropriate value back to Knack.


So, there we have it - a relatively simple way to check for uniqueness across multiple fields!


If you want to find out more about Knack click here.


or Integromat click here.




376 views2 comments

Recent Posts

See All

I am regularly asked about which No (or Low) Code platform to use for a particular customer requirement so I thought it would be interesting to write a post going through a couple of recent case studi