Expand all | Collapse all

Iterate JS code in a form

  • 1.  Iterate JS code in a form

    Posted 06-18-2019 10:50
    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:
      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_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){
    if(QB_newQty < QB_reqQty){
    //alert("not enough qty");
    QB_flagTrigger = $("#_fid_55").val();
    } else {
    alert("Invalid Barcode" + "\n" + "Please rescan");
    QB_flagTrigger = $("#_fid_55").val();
    async function IterateLoop() {
    while (QB_flagTrigger==1){
    await FieldUpdates();
    .then(() => alert("done and Qty = " + QB_newQty))
    .catch((err) => alert("error"));

    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.

  • 2.  RE: Iterate JS code in a form

    Posted 06-18-2019 23:38
    Can you please share the way you setup the form rule?

  • 3.  RE: Iterate JS code in a form

    Posted 06-19-2019 01:25
    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.

  • 4.  RE: Iterate JS code in a form

    Posted 06-19-2019 02:25
    Is the checkbox, Fire "change" actions only when a condition changes from false to true, checked or unchecked? 

  • 5.  RE: Iterate JS code in a form

    Posted 06-19-2019 02:27

  • 6.  RE: Iterate JS code in a form

    Posted 06-19-2019 02:35
    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.

  • 7.  RE: Iterate JS code in a form

    Posted 06-19-2019 03:33
    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?

  • 8.  RE: Iterate JS code in a form

    Posted 06-19-2019 12:30
    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. 

  • 9.  RE: Iterate JS code in a form

    Posted 06-19-2019 13:40
    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.

  • 10.  RE: Iterate JS code in a form

    Posted 06-20-2019 03:41
    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.