Iterate JS code in a form

  • 0
  • 1
  • Question
  • Updated 4 weeks ago
  • In Progress
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.
Photo of User56842

User56842

  • 110 Points 100 badge 2x thumb

Posted 4 weeks ago

  • 0
  • 1
Photo of Micro Dev

Micro Dev

  • 1,576 Points 1k badge 2x thumb
Can you please share the way you setup the form rule?
Photo of User56842

User56842

  • 110 Points 100 badge 2x thumb
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.
Photo of Micro Dev

Micro Dev

  • 1,514 Points 1k badge 2x thumb
Is the checkbox, Fire "change" actions only when a condition changes from false to true, checked or unchecked? 
Photo of User56842

User56842

  • 110 Points 100 badge 2x thumb
Unchecked.
Photo of Micro Dev

Micro Dev

  • 1,514 Points 1k badge 2x thumb
I would try to add another rule in the form that whenever that checkbox is checked, change it to unchecked and change the Fire "change" actions only when a condition changes from false to true. on both of these rules to checked.

Not sure it will work but just another try which I would do.
Photo of User56842

User56842

  • 110 Points 100 badge 2x thumb
Thanks for the effort but it still does the same thing. First run is good and all fields reset as coded but the JS is still not triggered a second time even though the form shows that the flags have changed correctly. Does QB not consider multiple changes to the same field as an actual change to the field after the first change if there isn't a save between changes?
Photo of Micro Dev

Micro Dev

  • 1,514 Points 1k badge 2x thumb
It should consider it as a change even without saving. I had the same thing with a trigger to show a msg and it only did it once, but after following the above, it did work. 
Photo of Harrison Hersch (QB)

Harrison Hersch (QB), Quick Base Sr. Product Manager (Platform)

  • 1,530 Points 1k badge 2x thumb
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.
Photo of User56842

User56842

  • 110 Points 100 badge 2x thumb
We are doing a count of a scanned item where the user only has to scan the barcode and the form takes care of verifying it is the right item, increments the scan count, and clears the entry field used for scanning. When the quantity scanned = required quantity, they save the record and continue to the next item. The scanner has been programmed to add a tab after each scan to trigger the form rules. The main purpose of the code is to focus the cursor back onto the scanning field after each scan.

I'm currently unaware of any native way to set focus back to a specific field without any user input other than Javascript. If there is a native and easier way to do those actions, please let me know. It that would be a really useful form rule though. E.g. If a flag field is true, set focus to field x.