Previous Record problem

Viewing 11 reply threads
  • Author
    Posts
  • December 27, 2023 at 5:53 PM #50246

    Glen Forister
    Participant

    I tried to use the script from my rain totals script to do a similar job, but I’m missing something to be abel to add up the record totals, to an accumulation of the record totals.

    Hope this is a simple fix.  See attached archive file with dummy data – just getting started.

    Attachments:
    You must be logged in to view attached files.
    December 27, 2023 at 7:09 PM #50248

    Glen Forister
    Participant

    FYI, this will enable me to keep track of the time I spend identifying insects taken from the caves in the Great Basin Nat. Park.  When I start work on them I never know how long I will be able to spend on the job or if I will be able to get back to it or not during the day.  I would like to know how long the job eventually takes.  This is just for info, since I’m not charging anything for this, but I get to look at some interesting insects and try to identify them.  Luckily I’m retired….

    December 27, 2023 at 7:21 PM #50249

    Daniel Leu
    Participant

    This works for me:

    function toHrsMin(sec){
       let h = Math.floor(sec / 3600);
       let m = (sec / 60) % 60;
       return h + ' hr, ' + m + ' mins';
    }
    function Accum_Tot() {
       var records = form.getRecords();
       var currentRecordIndex = records.indexOf(record);
       if (currentRecordIndex > 0) {
          var previousRecord = records[currentRecordIndex-1];
      var previous_total = previousRecord.getFieldValue('fld-44acc9d2314f41beb3f2257ace5bae01');
       } else {
          var previous_total = 0;
       }
       var today = record.getFieldValue('fld-44acc9d2314f41beb3f2257ace5bae01');
       var total = today + previous_total;
       console.log("Today: " + toHrsMin(today))
       console.log("Previous: " + toHrsMin(previous_total))
       console.log("Total: " + toHrsMin(total))
       return total;
    }
    Accum_Tot();
    • This reply was modified 10 months, 3 weeks ago by Daniel Leu.
    • This reply was modified 10 months, 3 weeks ago by Daniel Leu.
    • This reply was modified 10 months, 3 weeks ago by Daniel Leu.
    • This reply was modified 10 months, 3 weeks ago by Daniel Leu.
    • This reply was modified 10 months, 3 weeks ago by Daniel Leu.
    • This reply was modified 10 months, 3 weeks ago by Daniel Leu.
    • This reply was modified 10 months, 3 weeks ago by Daniel Leu.
    December 27, 2023 at 7:27 PM #50254

    Brendan
    Keymaster

    You could also just have Tap Forms calculate the totals for you. See screenshot.

    Attachments:
    You must be logged in to view attached files.
    December 28, 2023 at 11:12 AM #50260

    Glen Forister
    Participant

    Thanks Daniel, I like your solution the best but it adds the first two records then subtracts any following numbers which isn’t helpful.

    Thanks Brenden, I don’t really like the total showing accumulating, only at the bottom, but it does work.  But I don’t see the total growing which I would like – that is a double check for me.

    December 29, 2023 at 12:40 AM #50262

    Brendan
    Keymaster

    You can have it display section totals on the Mac version too. But it’s only the totals for that specific section, not a growing total.

    December 29, 2023 at 10:50 AM #50263

    Glen Forister
    Participant

    That is why I would prefer to use the script solution.  Unfortunately, what Daniel supplied:

    But, it adds the first two records then subtracts any following numbers which isn’t helpful.

    I’ve looked at the script, but don’t understand how it jumps out of the loop and starts subtracting instead of adding to the total.

    Can you please give me a script that works?

    December 30, 2023 at 5:45 AM #50268

    Daniel Leu
    Participant

    Hi Glen, in your original script, you were reading the previous total from the wrong field. Now it should work as expected:

    function toHrsMin(sec){
    let str = "";
    let h = Math.floor(sec / 3600);
    let m = (sec / 60) % 60;
    return h + ' hr, ' + m + ' mins';
    }
    function Accum_Tot() {
    var records = form.getRecords();
        var currentRecordIndex = records.indexOf(record);
        if (currentRecordIndex > 0) {
            var previousRecord = records[currentRecordIndex-1];
            var previous_total = previousRecord.getFieldValue('fld-b2f9616bf5e44fb4880ad9addd2afc6e');
        } else {
        var previous_total = 0;
        }
    var today = record.getFieldValue('fld-44acc9d2314f41beb3f2257ace5bae01');
    var total = today + parseInt(previous_total);
    console.log("Today: " + toHrsMin(today))
    console.log("Previous: " + toHrsMin(previous_total))
    console.log("Total: " + toHrsMin(total))
    return total;
    }
    Accum_Tot();
    • This reply was modified 10 months, 3 weeks ago by Daniel Leu.
    December 30, 2023 at 7:47 AM #50270

    Glen Forister
    Participant

    I wondered about that, but I couldn’t find the “previous” and I still can’t find where you got:

    ‘fld-b2f9616bf5e44fb4880ad9addd2afc6e

    I just thought it was the same field, but just the previous record of that field.  Hint please?

    Thanks.

    December 30, 2023 at 8:07 PM #50272

    Daniel Leu
    Participant

    Yeah, it’s a bit tricky. In the form inspector panel,  select the field tab followed by the ‘previous’ field. Then scroll down. Underneath the Description field is the field ID.

     

    Attachments:
    You must be logged in to view attached files.
    December 31, 2023 at 10:32 AM #50274

    Glen Forister
    Participant

    Thanks, I’ve save that for later use in my TF file of questions and answers for easy searching.

    January 28, 2024 at 10:35 AM #50401

    Glen Forister
    Participant

    I was getting the ID numbers from the right spot, but just using the wrong Script.

    At least I got the one I need to work working, the old one doesn’t want to work, but no time for that and it was primarily there as an example anyhow.

    Thank.

Viewing 11 reply threads

You must be logged in to reply to this topic.