Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Help with NaN script result in a table
Tagged: NaN
- This topic has 6 replies, 2 voices, and was last updated 4 months, 1 week ago by Daniel Leu.
-
AuthorPosts
-
August 20, 2024 at 12:08 AM #51011
tapforms_userParticipantHi I’m new to Tapforms and really enjoying the product.
I’m having some difficulty with a script to run in a table. It should run the calculation on column name “G2” and then return a number value in column “G” but instead it populates NaN in the fields of column G. I have checked that column G2 is set to field, which it is, and the values in column G are all numbers. Column G is of course set as a script.
Can anyone perhaps see where I have gone wrong? Or perhaps this is a common error with a known tip to correct it?
Edit: I added the script as an attachment in a text file as I am not sure how to copy it in here.
- This topic was modified 4 months, 1 week ago by tapforms_user. Reason: Added script in text file attachment
- This topic was modified 4 months, 1 week ago by tapforms_user.
- This topic was modified 4 months, 1 week ago by tapforms_user.
Attachments:
You must be logged in to view attached files.August 20, 2024 at 6:36 AM #51016
tapforms_userParticipantI mean I have checked that column G is set to a “Number” field.
- This reply was modified 4 months, 1 week ago by tapforms_user. Reason: Typo fix
August 20, 2024 at 7:49 AM #51020
Daniel LeuParticipantThere are two issues I see: “var value = record.getFieldValue(‘G2’);” and “record.setFieldValue(‘G’, resultG);”. Both functions expect a field id not a field name. So either you change it to “getFieldValue(‘fld-xxxx’)” with ‘xxxx’ being the number you get in the editor (click on the field, then ID), or use “record.getFieldValue(form.getFieldNamed(‘G2’).getId(), resultG);”, same for
record.setFieldValue();
.Additionally, after you set the value, you have to add a
document.saveAllChanges();
to get your results into the database.Hope this helps.
- This reply was modified 4 months, 1 week ago by Daniel Leu.
- This reply was modified 4 months, 1 week ago by Daniel Leu.
- This reply was modified 4 months, 1 week ago by Daniel Leu.
August 20, 2024 at 1:06 PM #51024
tapforms_userParticipantThere are two issues I see: “var value = record.getFieldValue(‘G2’);” and “record.setFieldValue(‘G’, resultG);”. Both functions expect a field id not a field name. So either you change it to “getFieldValue(‘fld-xxxx’)” with ‘xxxx’ being the number you get in the editor (click on the field, then ID), or use “record.getFieldValue(form.getFieldNamed(‘G2’).getId(), resultG);”, same for
record.setFieldValue();
. Additionally, after you set the value, you have to add adocument.saveAllChanges();
to get your results into the database. Hope this helps.This is a huge help. Thank you! I tested your recommendations using another more simple script to concatenate different fields in the table – I had also been struggling with this. Switching to ID and adding the save changes fixed everything! Thank you! I had been battling with this for hours.
How can I get the script to run automatically? I.e. to populate the destination field ID when data is entered into the fields it is pulling the data from.
August 20, 2024 at 2:35 PM #51025
Daniel LeuParticipantGreat that you got it working!
To run the script automatically, you would need to create a field script. It’s just like another field, but has the script attached to it. Usually with a field script, you wouldn’t update a field in all records. Depending on the number of records, you might notice a performance issue.
August 22, 2024 at 4:39 PM #51033
tapforms_userParticipantHi coming back to the more simple script. It’s working great thanks to your help. Now I would like to also add (concatenate) some fields from another table (but on the same form). I’ve tried using the field ID’s from both tables but it returns 1 or undefined.
What I’d like to know is: Is it possible to concatenate multiple fields from 2 different tables? if yes, what must I add to the script?
August 22, 2024 at 10:36 PM #51034
Daniel LeuParticipantSomething like this:
function xxx (){ var a = record.getFieldValue('fld-xxx'); var b = record.getFieldValue('fld-xxx'); var c = a + " some text " + b; console.log("Result: " + c); ... }
The ‘+’ is used to concatenate strings.
- This reply was modified 4 months, 1 week ago by Daniel Leu.
- This reply was modified 4 months, 1 week ago by Daniel Leu.
- This reply was modified 4 months, 1 week ago by Daniel Leu.
-
AuthorPosts
You must be logged in to reply to this topic.