Previous record skip

Viewing 4 reply threads
  • Author
    Posts
  • March 11, 2024 at 11:04 AM #50622

    Glen Forister
    Participant
    I can’t figure out how to fix the code so the first actual record after the “Begin Year Mark” record adds the $100 to $0 of the initialization record for the year.
    Thanks…
    Date              Org         $     Total$
    01/01/24    Begin.      0         0            Begin Year mark
    01/01/24.   KVMR   100      0
    03/12/24    test          20      20
    03/27/24.  test.2.      50      70
    04/17/24.  test.3.      55     125
    Can’t remember how to display the code correctly – searched for it – Put that hint somewhere? “!”???
    !
    function Year_to_date() {
    var date_id = ‘fld-e2d20da877cb4a6c8fd72153b86f1ab1’;
    var donation_id = ‘fld-05c429d6a4484061b554aa584a45f8fc’;
    var total_id = ‘fld-7c6b9bb288ab47e0b295af6eacc9cd26’;
    var records = form.getRecords();   // calls function getRecords()
        var currentRecordIndex = records.indexOf(record);
        if (currentRecordIndex > 0) {
            var previousRecord = records[currentRecordIndex-1];
        } else {
          return 0;
        }
    // Is this the beginning of the year?
    var date = record.getFieldValue(date_id);
    if (date.getMonth() == 0 && date.getDate() == 1){
    return 0;
    }
    var today = record.getFieldValue(donation_id);
    var previous_total = previousRecord.getFieldValue(total_id);
    var total = today + previous_total;
    console.log(“Today: ” + today)
    console.log(“Previous: ” + previous_total)
    console.log(“Total: ” + total)
    return total;
    }
    Year_to_date();
    !
    March 11, 2024 at 11:07 AM #50623

    Glen Forister
    Participant
    I can’t figure out how to fix the code so the first actual record after the “Begin Year Mark” record adds the $100 to $0 of the initialization record for the year.
    Thanks…
    Date Org $ Total$
    01/01/24 Begin. 0 0 Begin Year mark
    01/01/24. KVMR 100 0
    03/12/24 test 20 20
    03/27/24. test.2. 50 70
    04/17/24. test.3. 55 125
    Can’t remember how to display the code correctly – searched for it – Put that hint somewhere? “!”???
    !
    function Year_to_date() {
    var date_id = ‘fld-e2d20da877cb4a6c8fd72153b86f1ab1’;
    var donation_id = ‘fld-05c429d6a4484061b554aa584a45f8fc’;
    var total_id = ‘fld-7c6b9bb288ab47e0b295af6eacc9cd26’;
    var records = form.getRecords(); // calls function getRecords()
    var currentRecordIndex = records.indexOf(record);
    if (currentRecordIndex > 0) {
    var previousRecord = records[currentRecordIndex-1];
    } else {
    return 0;
    }
    // Is this the beginning of the year?
    var date = record.getFieldValue(date_id);
    if (date.getMonth() == 0 && date.getDate() == 1){
    return 0;
    }
    var today = record.getFieldValue(donation_id);
    var previous_total = previousRecord.getFieldValue(total_id);
    var total = today + previous_total;
    console.log(“Today: ” + today)
    console.log(“Previous: ” + previous_total)
    console.log(“Total: ” + total)
    return total;
    }
    Year_to_date();
    !
    March 11, 2024 at 2:20 PM #50624

    Daniel Leu
    Participant

    Assuming that the beginning of the year mark has always 0 as the donation value, you could use this to bypass the initialization:

    // Is this the beginning of the year?
    var date = record.getFieldValue(date_id);
    if (date.getMonth() == 0 && date.getDate() == 1){
       if (record.getFieldValue(donation_id) == 0){
          return 0;
      }
    }
    March 11, 2024 at 2:42 PM #50625

    Glen Forister
    Participant

    I was already using a verson of that, but it was missing a line.

    if (record.getFieldValue(donation_id) == 0){

    When I added that line it works.  Thanks.

    You didn’t tell me what to use to make code format correctly here.  I searched for it but couldn’t find it.

    March 11, 2024 at 2:55 PM #50626

    Brendan
    Keymaster

    Use the back tick character to surround your code:

     

    
    if (record.getFieldValue(donation_id) == 0){
    
    
Viewing 4 reply threads

You must be logged in to reply to this topic.