Simple previous

Viewing 8 reply threads
  • Author
    Posts
  • March 4, 2023 at 3:09 PM #49082

    Glen Forister
    Participant

    I’ve applied your “previous_field” script to several Forms and been quite successful, but this is about as simple as you can get and I can’t figure out what I’ve missed. See attached.

    Also, does it matter where the script is used: 1) the script in the field window or 2) the script in the Script tab? Is there any difference between the two in the code?

    Attachments:
    You must be logged in to view attached files.
    March 4, 2023 at 9:31 PM #49084

    Daniel Leu
    Participant

    1) There is a missing closing curly bracket after return 0;.

    2) This script is a field script and not a form script.

    • This reply was modified 1 year, 2 months ago by Daniel Leu.
    • This reply was modified 1 year, 2 months ago by Daniel Leu.
    March 5, 2023 at 11:13 AM #49087

    Glen Forister
    Participant

    Thanks.
    That closing bracket did allow it to run.

    Unfortunately, it returns NaN every record.
    I checked and changed $/grp from currency to a number with 2 decimal places and the result is the same.
    I checked and made sure all 4 number fields were formatted = number (either 0 or 2 decimal places).

    Not sure what I’ve forgotten now. See attached.

    Attachments:
    You must be logged in to view attached files.
    March 5, 2023 at 8:24 PM #49089

    Daniel Leu
    Participant

    I would add console.log($_dwr); to see where NaN might originate.

    • This reply was modified 1 year, 2 months ago by Daniel Leu.
    March 6, 2023 at 1:08 AM #49092

    Brendan
    Keymaster

    The Number Formatter setting you use on your Number fields will make no difference to your script. Tap Forms just sees those values as raw numbers without formatting anyway when being processed in a script.

    You are missing a semi-colon at the end of your console.log statement though. Not that that would cause the NaN error.

    Does your $/grp field return a valid number from its calculation? Is its formula set to return a Number result type?

    March 6, 2023 at 12:33 PM #49094

    Glen Forister
    Participant

    I played with the script with different changes and it appears I think that,
    “Tot$/dwr: ReferenceError: Can’t find variable: previousRecord_total, line:(null)”

    This is past my diagnostic ability now, so I’ll attach the archive to save us both a lot of time possibly.
    Thanks.

    PS, I think I’m almost done getting my life back on track with functioning forms again after moving from HanDBase to TF. Now, I’ll try to researching JavaScript to see if I can get a handle on the basics which the preliminary videos and files just hinted at. Too much to learn quickly.

    Attachments:
    You must be logged in to view attached files.
    March 6, 2023 at 1:34 PM #49097

    Daniel Leu
    Participant

    I don’t really understand what you would like to calculate. But here are my 2 cents:

    You get the error:
    > “Tot$/dwr: ReferenceError: Can’t find variable: previousRecord_total, line:(null)”
    because the variable previousRecord_total is not defined. It should be previousTotal and the statement should be a line lower.

    > var previous_total = previousRecord.getFieldValue($_grp);`
    getFieldValue() needs a field id and not a value.

    For the previous calculation, the field id is the id of the script. You can get it from the form inspector panel > field > select field and look for the ID under the description section.

    I would change as well how the first initial value for currentRecordIndex == 0 is handled:

    var previous_total = 0;
    if (currentRecordIndex > 0) {
       var previousRecord = records[currentRecordIndex-1];    
       previous_total = previousRecord.getFieldValue(totalId);
    }
    

    Now previous_total is initialized to 0. If there is an earlier record, it is set accordingly.

    With these changes, my field script now looks like this:

    function Add_Previous() {
        var $_dwr = record.getFieldValue('fld-c1faad7124f8400bbf9e55c477b101ef');
        var $_grp = record.getFieldValue('fld-72156bcc2515453d80a4649d30cb938f');
        var totalId = 'fld-9bc44cd8a52547fcab897dbfa04afcc2';
    	
        var records = form.getRecords();   // calls function getRecords()
        var currentRecordIndex = records.indexOf(record);
    
        var previous_total = 0;
        if (currentRecordIndex > 0) {
           	var previousRecord = records[currentRecordIndex-1];    
           	previous_total = previousRecord.getFieldValue(totalId);
        }
        
        console.log("dwr: " + $_dwr);
        console.log("grp: " + $_grp);
        console.log("previous total: " + previous_total);
       	
        var total = $_dwr + previous_total;	
        console.log("Total: " + total);
    	
        return total;
    }
    
    Add_Previous();

    Hope this helps!

    March 6, 2023 at 2:36 PM #49099

    Glen Forister
    Participant

    Yes! You got it right, that is perfect.
    Thanks. I’ll study this.

    March 6, 2023 at 3:52 PM #49101

    Daniel Leu
    Participant

    Great!

Viewing 8 reply threads

You must be logged in to reply to this topic.