Skip to main content
Question

Iterate JS code in a form

  • June 18, 2019
  • 0 replies
  • 87 views

I'm trying to have JS run in the background of a form in a loop that sets a couple of field values and then refocuses back to the original entry field (user is using a barcode scanner which is programmed to execute a tab after each scan).

First off, this is a form editing an existing record (always) and second, the record can't be saved until the user has completed multiple scans. So no saving and refreshing a page to reload the data/reset triggers.

All of the code works fine when the form rules trigger the IOL field to run the JS module. However, after it finishes executing once and resets the form fields back to the starting conditions, it will not execute a second time even though the field with the IOL formula has reset.

Does anybody know how to trigger JS multiple times on a single form without saving between each run and without user interaction (no button clicks) or how I could get the code to run in an async/await method to run as a loop in the background of the form?

Code below:
(function(){
  var querystring=document.location.search;
if (/a=er&dfid=11/i.test(querystring)) {
var QB_flagTrigger=$("#_fid_55").val();
var QB_barcodeOld;
var QB_barcodeNew;
var QB_newQty=0;
var QB_reqQty=0;
function FieldUpdates() {
QB_reqQty=parseInt($("#_fid_60").val(),10)
QB_newQty =parseInt($("#_fid_31").val(),10)+1;
QB_barcodeOld = parseInt($("#_fid_56").val(),10);
QB_barcodeNew = parseInt($("#_fid_30").val(),10);
if(QB_barcodeOld == QB_barcodeNew){
//alert("valid");
$("#_fid_31").val(QB_newQty);
$("#_fid_30").val("");
if(QB_newQty < QB_reqQty){
//alert("not enough qty");
$("#_fid_55").val(3);
}
QB_flagTrigger = $("#_fid_55").val();
} else {
//alert("invalid");
$("#_fid_30").val("");
alert("Invalid Barcode" + "\n" + "Please rescan");
$("#_fid_55").val(3);
QB_flagTrigger = $("#_fid_55").val();
}
return;
}
async function IterateLoop() {
while (QB_flagTrigger==1){
await FieldUpdates();
}
}
IterateLoop()
.then(() => alert("done and Qty = " + QB_newQty))
.catch((err) => alert("error"));
document.getElementById("_fid_30").focus();
document.getElementById("_fid_30").select();
}
})();

Note that some of the above code was my testing async/await functionality in QB to see if I could put the FieldUpdate function inside of an async function and have the JS load with the form and constantly requery the fields and execute when needed. I probably screwed something up there since I'm new at async and during testing the page ran the loop without actually loading the form for the user to use.

Thanks in advance for any help.
This topic has been closed for replies.

Can you please share the way you setup the form rule?

It's just a simple rule that checks to see if the value in the quantity scanned field is equal to the required quantity value. If not equal, then it sets a checkbox flag that the field containing the IOL code is looking at to determine whether to run or not.

Honestly, the form rule isn't needed as I can set the IOL formula field to look at the scanned quantity compared to the required quantity and it would have the same result. That was the way I set it up originally but had the same problem it's having now, runs once then never triggers again.

Forum|alt.badge.img+11
  • Quickbase Staff
  • June 19, 2019
Hi. Would you like to share the core things you are trying to solve with this? There might be native ways that are more durable and supported.