AdministratorIT
6 years agoQrew Trainee
Iterate JS code in a form
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.
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.