Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Repurpose previous record scipt
- This topic has 18 replies, 3 voices, and was last updated 1 year, 10 months ago by Brendan.
-
AuthorPosts
-
February 15, 2023 at 3:32 PM #48912
Glen ForisterParticipantThanks for the script for rain totals. Now I figure I can make that do the same for donations. Am I on the right track? I keep getting syntax errors or other errors trying to get the syntax right. Am I way off track? Attached is my screen shot. I can’t seem to get the {} deletions right.
– I deleted the “beginning of the year” section.
– Replaced the variables “today” & “previous_total” ID values with the ones appropriate for my form. Now it looks like this.function Season_to_date() { var records = form.getRecords(); var currentRecordIndex = records.indexOf(record); if (currentRecordIndex > 0) { var previousRecord = records[currentRecordIndex-1]; } else { return 0; // xx = line deleted xx } xx // Is this the beginning of the year? xx var date = record.getFieldValue('fld-0720a5baa6fc4980aee22798bd089714'); xx if (date.getMonth() == 9 && date.getDate() == 1){ xx return 0; } var today = record.getFieldValue('xxxxx'); //replaced val with my value from form var previous_total = previousRecord.getFieldValue('fld-xxxxx'); //replaced val with my value from form var total = today + previous_total; console.log("Today: " + today) console.log("Previous: " + previous_total) console.log("Total: " + total) return total; } Season_to_date();
- This topic was modified 1 year, 10 months ago by Brendan. Reason: Added back ticks around code -- Brendan
Attachments:
You must be logged in to view attached files.February 15, 2023 at 3:56 PM #48916
BrendanKeymasterGlen, if you put a back tick character before and after your code, the forum will format it properly as a code block for you.
February 15, 2023 at 4:01 PM #48918
BrendanKeymasterYou’re defining your
previousRecord
variable inside theif
statement. Variables have scope. That means they can only be seen inside the code block where you define them.if (currentRecordIndex > 0) { var previousRecord = records[currentRecordIndex-1]; }
You’ll want to do this:
var previousRecord; if (currentRecordIndex > 0) { previousRecord = records[currentRecordIndex-1]; }
But it would be helpful if you posted the syntax errors you’re seeing. All those
xx
in the code would definitely cause a syntax error.February 15, 2023 at 5:43 PM #48921
Glen ForisterParticipantThat didn’t help. I get error in line 16 and playing with that I’m not sure where I’m at.
Added a backtick char.Here is the code I was using for my rain totals, but the rain season was from Oct 1 to Sept 31 of each year which required a loop I think to check for the end of the season. Since for this purpose, the year I’m grouping is for the tax year Jan 1 to Dec 31. So, didn’t think I needed the search for Oct 1 date. I thought if I took out that portion that this script would do the job of letting me see all years worth of donations and set up a view of only one specific year and see donations starting at 0 and adding up the amounts until Dec 31. Seemed simple.
This is the Rain script, so I assume I have to replace the id code like this (‘fld-0720a5baa6fc4980aee22798bd089714) with the id code for the field I want in my donation form.
So, do I have to start over or can I use this code with the correct changes. Sorry, thought this would be easy. Hopeful and thank you.
================function Season_to_date() { var records = form.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('fld-0720a5baa6fc4980aee22798bd089714'); if (date.getMonth() == 9 && date.getDate() == 1){ return 0; } var today = record.getFieldValue('fld-61c03b767f2c497491d10a28db8abdb3'); var previous_total = previousRecord.getFieldValue('fld-f6bc7e82da874579940fb8afc167dac4'); var total = today + previous_total; console.log("Today: " + today) console.log("Previous: " + previous_total) console.log("Total: " + total) return total; } Season_to_date();
- This reply was modified 1 year, 10 months ago by Glen Forister.
February 15, 2023 at 6:06 PM #48924
Daniel LeuParticipantBrendan, AFAIK, what you described is the scope for
let
and not forvar
.February 15, 2023 at 6:26 PM #48925
Daniel LeuParticipantYou still need to reset the year-to-date field at the beginning of the year.
if (date.getMonth() == 9 && date.getDate() == 1){ return 0; }
Needs to be
if (date.getMonth() == 0 && date.getDate() == 1){ return 0; }
The function should look like this:
function Season_to_date() { var records = form.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('fld-0720a5baa6fc4980aee22798bd089714'); if (date.getMonth() == 0 && date.getDate() == 1){ return 0; } var today = record.getFieldValue('fld-61c03b767f2c497491d10a28db8abdb3'); var previous_total = previousRecord.getFieldValue('fld-f6bc7e82da874579940fb8afc167dac4'); var total = today + previous_total; console.log("Today: " + today) console.log("Previous: " + previous_total) console.log("Total: " + total) return total; } Season_to_date();
And again, this only works if there is a record for January 1st.
February 15, 2023 at 7:40 PM #48926
BrendanKeymasterOh that’s weird. I didn’t know JavaScript had different scope rules than most every other programming language I know.
February 15, 2023 at 7:47 PM #48927
Glen ForisterParticipantI have all my Jan 1 records created.
Here is the error:2/15/23, 6:45:44 PM / Donations / Yearly
Yearly: TypeError: undefined is not an object (evaluating ‘date.getMonth’), line:(null)Thanks for looking.
February 15, 2023 at 7:51 PM #48928
Glen ForisterParticipantThe TF crashed. When it opened and I ran refresh again.
I get no errors:
2/15/23, 6:48:31 PM / Donations / YearlyBut, all my values in the Total col are blank.
Would help if the script could be run line by line. I assume it can, but haven’t found it.
- This reply was modified 1 year, 10 months ago by Glen Forister.
February 15, 2023 at 8:34 PM #48930
BrendanKeymasterScripts can’t be run line by line due to how Apple’s JavascriptCore framework works.
The best you can do is to put
console.log("got here")
orconsole.log(someVariable)
statements into your code.But when you see
undefined
, it means exactly that. The value you’re trying to call a method on is empty or hasn’t been initialized with a value.So you should add
date != undefined
into yourif
statement where you callgetMonth()
.if (date != undefined && date.getMonth() == 0 && date.getDate() == 1){ }
You may have a record that has no date value, so that’s what would cause that error.
February 16, 2023 at 10:34 AM #48932
Glen ForisterParticipantI did have a record that had no data, date or values. Deleted that. Also made sure my added records for Jan 1 had the value = 0 for donations (that field was empty).
Now the script runs without errors with the original lines, or those lines replaced with the above correction for undefined.But, all my records have nothing in the Totals value. All blank.
I checked the script window and the value = number. Also the field Total is calculated and that value = decimal with zero decimal places.
Stopped the program, loaded it again and brought up the Form and refreshed it many times and no totals show up.
Is there a way to do this?
Thanks. Code below just so you know what I’m working with.function Season_to_date() { var records = form.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('fld-0720a5baa6fc4980aee22798bd089714'); if (date != undefined && date.getMonth() == 0 && date.getDate() == 1){ } // if (date.getMonth() == 0 && date.getDate() == 1){ // return 0; //} var today = record.getFieldValue('fld-61c03b767f2c497491d10a28db8abdb3'); var previous_total = previousRecord.getFieldValue('fld-f6bc7e82da874579940fb8afc167dac4'); var total = today + previous_total; console.log("Today: " + today) console.log("Previous: " + previous_total) console.log("Total: " + total) return total; } Season_to_date();
I just ran it again to verify and I get using one statement:
2/16/23, 9:26:49 AM / Donations / Yearly
Yearly: TypeError: undefined is not an object (evaluating ‘date.getMonth’), line:(null)The other statement gives me:
2/16/23, 9:29:25 AM / Donations / Yearly
Today: undefined
Previous: undefined
Total: NaNI’m really confused. Don’t know what is going on.
Here is my file.
I would like to see my donations for any single year that I can see with a search view.
I have a script in the Total Field and a similar one (maybe different now) in the Script designer field.Attachments:
You must be logged in to view attached files.February 16, 2023 at 11:57 AM #48934
Daniel LeuParticipantYou need to select ‘script’ and not ‘calculation’ for the field type, return type is ‘number’.
The script had some errors too like incorrect field ids.
function Season_to_date() { var date_id = 'fld-e2d20da877cb4a6c8fd72153b86f1ab1'; var donation_id = 'fld-05c429d6a4484061b554aa584a45f8fc'; var total_id = 'fld-7c6b9bb288ab47e0b295af6eacc9cd26'; var records = form.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; } Season_to_date();
February 16, 2023 at 12:30 PM #48935
Glen ForisterParticipantWhile working with it I noticed the IDs were wrong also as you pointed out. Don’t know how that happened.
I’ll have to compare this script with the previous one to see what you did, but with the changes you mentioned in the program and your last script, it worked.
Thanks for the work. Hopefully I can transfer this to other forms and make it work there also.
February 16, 2023 at 2:42 PM #48936
Daniel LeuParticipantGreat that it works for you too!
Yeah, to make it easier I consolidated all the field IDs at the top. To get the
total_id
, which is the ID of the script field, you need to close the script editor and look at the form editor. See attachment. You can get all the other IDs from the left side panel in the script editor.Attachments:
You must be logged in to view attached files.February 16, 2023 at 2:59 PM #48938
Glen ForisterParticipantI didn’t know it was there. I’ve been using the Script Editor to get those inserted.
Thanks.I appreciate the way you organized it. That is a lot easier to read.
I’m having trouble reading and understanding the objects, etc. and have to study a bit more to understand it. When done I’ll attempt another use for the code finding out how many days between the date of the last treatment of the previous entry and the date of the current entry and record that in a Days between treatments.
I have other work to get done first though. Even though I’m 79, I can’t get enough done with my insect collection and prepare for another season of collecting. Being retired is a lot of work.
February 16, 2023 at 3:08 PM #48939
Glen ForisterParticipantI thought I was done. I was double checking that Script field ID, and wondered why it didn’t show up in the script editor and then forgot about that.
I hit refresh and now I have the totals incrementing, but instead of adding the previous total to the current number, it is concatenating them.
One of the things I love about programming is the surprises you don’t expect because of flawed logic…
0 0
50 500
100 100500
100 100100500
100 100100100500
20 20100100100500
60 6020100100100500February 16, 2023 at 3:36 PM #48940
Daniel LeuParticipantAs I mentioned before, the return type of the script needs to be set to
number
.- This reply was modified 1 year, 10 months ago by Daniel Leu.
February 16, 2023 at 3:55 PM #48942
Glen ForisterParticipantTerribly sorry, I don’t know how that got changed.
That did it.
I need a check list of all the things to check before I scream help.Interesting, I see how adding text would concatenate the value if text. Nice clue.
- This reply was modified 1 year, 10 months ago by Glen Forister.
February 17, 2023 at 2:48 AM #48944
BrendanKeymasterJust adding text together is very handy for doing things like creating a Full Name field by doing something like
first_name + " " + last_name
. -
AuthorPosts
You must be logged in to reply to this topic.