Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Calling a field value from a record or another form
- This topic has 13 replies, 5 voices, and was last updated 4 years, 11 months ago by Brendan.
-
AuthorPosts
-
November 5, 2019 at 9:28 AM #37807
EddyParticipantDear Folks!
I don’t know, if this is a bug of if I am to stupid…
In a script I have to call some field values.
For fields in the same form this is no problem.
record.getFieldValue(…)But if I call a field from another form, I get a error message.
The editor produces this phrase to call the value:
“XXXX.getFieldValue(…)”
“XXXX” is the name of the form, in which is the field, we need the value from.The error message is: “ReferenceError: Can’t find variable”
Where is the mistake?
Thanks in advance,
EddyP.S: My momentary workaround is to create a calculation field in my form, that calls the field value that I need and than to refer in the script to that calculation field. Working, but not really clean.
November 5, 2019 at 10:30 AM #37816
Daniel LeuParticipantHmmm, did you create the form object with
var XXXX = document.getFormNamed('form name');
? Or are the two forms linked?November 6, 2019 at 6:08 PM #37884
EddyParticipantHi Daniel
Thanks for answering.
Indeed, the two forms are linked. Look at my initial posting. I tried to explain the syntax of the phrase I got, when I used the built in snipplets. It seems, as if there is a problem to call a value from a record, that is in another form.
Any ideas?
Regards, Eddy
November 6, 2019 at 9:36 PM #37890
Sam MoffattParticipantYou need to assign a variable which is the record from the other form first.
In the Script Editor, double click on the form name to get it’d field ID and then use
var otherform = record.getFieldValue('fld-id')
to get the linked records. If this shows up in Tap Forms as a table then you’re going to get an array of records (1:M in the parent, M:M fields or JOIN fields), if you’re in the child of a 1:M relationship then you will get a single record (the parent).Then you can get the value of the fields from that record via the other form, e.g.
otherform[0].getFieldValue('fld-otherid')
.November 9, 2019 at 11:01 PM #37975
EddyParticipantHi Sam!
Thanks for your answer!
Helped and soluted my issue.
Regards, Eddy
January 3, 2020 at 8:18 AM #39071
Sonya WaltersParticipantHi,
I’m a very inexperienced programmer trying to use the scripts function to draw data from one form to another from via a linked field.
I would like to transfer the price of framing a work from one form (portfolio paintings) to another (Serendipity) so as to not have to enter the same data twice.
I searched the forums andfound the information in this thread to be relevant and useful.
However I have almost no experience in using programming languages so the line of code
referenced in the thread aboveotherform[0].getFieldValue('fld-otherid')
does not have enough information for me to implement it. The field I would like to pull in, is field number sixteen (starting from zero) in the linked form.
I find using “0” as the index does not return an error but “2”, “indexOf” and “16” all produce something like:
Script: TypeError: undefined is not an object (evaluating ‘portfoliopaintings[2].getFieldValue’), column:39, line:9
from the lines below:
var framecost = portfoliopaintings[index].getFieldValue(‘fld-nnn’);
// ^^
// error appears at this spotfunction FramePriceFromPortfolioDB() { //Get id of same work in matching linked portfoliopaintings form: var portfoliopaintings = record.getFieldValue('fld-mmmm'); //Find the Frame price of that record in portfoliopaintings var framecost = portfoliopaintings[index].getFieldValue('fld-nnn'); // ^^ // error appears at this spot return "framecost"; } FramePriceFromPortfolioDB();
Does anyone understand this question enough to tell me how to refer to the field in the linked record, and return its value?
Thank you for any help, and, happy new year!
SonyaJanuary 3, 2020 at 8:20 AM #39072
Sonya WaltersParticipantHi,
I’m a very inexperienced programmer trying to use the scripts function to draw data from one form to another from via a linked field.
I would like to transfer the price of framing a work from one form (portfolio paintings) to another (Serendipity) so as to not have to enter the same data twice.
I searched the forums andfound the information in this thread to be relevant and useful.
However I have almost no experience in using programming languages so the line of code
referenced in the thread aboveotherform[0].getFieldValue(‘fld-otherid’)
does not have enough information for me to implement it. The field I would like to pull in, is field number sixteen (starting from zero) in the linked form.
I find using “0” as the index does not return an error but “2”, “indexOf” and “16” all produce something like:
Script: TypeError: undefined is not an object (evaluating ‘portfoliopaintings[2].getFieldValue’), column:39, line:9
from the lines below:
var framecost = portfoliopaintings[index].getFieldValue(‘fld-nnn’);
// ^^
// error appears at this spotfunction FramePriceFromPortfolioDB() {
//Get id of same work in matching linked portfoliopaintings form:
var portfoliopaintings = record.getFieldValue(‘fld-mmmm’);//Find the Frame price of that record in portfoliopaintings
var framecost = portfoliopaintings[index].getFieldValue(‘fld-nnn’);
// ^^
// error appears at this spotreturn “framecost”;
}
FramePriceFromPortfolioDB();
Does anyone understand this question enough to tell me how to refer to the field in the linked record, and return its value?
Thank you for any help, and, happy new year!
SonyaJanuary 3, 2020 at 1:24 PM #39079
BrendanKeymasterHi Sonya,
I hope you don’t mind, but I put in a couple of tilde characters in your post to make your code more easily readable.
is
fld-mmmm
the Field ID for your Link to Form field?And what is the value of
index
when you use it?Can you please post the entirety of your script so we can have a better look at it?
Thanks,
Brendan
January 3, 2020 at 5:02 PM #39080
Sonya WaltersParticipantHi, this is Sonya again, thank you for your suggestions on how to better document my questions.
Here is my entire script (partially written by clicking on fields):
function FramePriceFromPortfolioDB() { //Get id of same work in portfoliopaintings using the link var portfoliopaintings = record.getFieldValue('fld-0b51acdae23a47789d7210379a6ddf2f'); //Find the Frame price of that record in portfolio paintings var framecost = portfoliopaintings[index].getFieldValue('fld-c9071aa3421e4715a6bda880b63a32a3'); return "framecost"; // return "3.00"; this was a test }
The string fld-0b51acdae23a47789d7210379a6ddf2f (mmmm in previous post) is the Field ID of the Link to Form Field (used in the join).
For index, I have tried inserting 16 (because the field I am looking to transfer is the sixteenth in the joined form Portfoliopaintings), as well as 2, and for kicks, 0.
But I am not sure how to fill the index string. In case it helps I attached a screen shot of the fields in my form, including the linked form. It doesn’t show all the fields in the linked form, in the screenshot, but the one I want is number sixteen, a numeric value.
I hope this makes sense, and.
Thank you for your help!
Attachments:
You must be logged in to view attached files.January 3, 2020 at 5:35 PM #39083
Sam MoffattParticipantThe
portfoliopaintings
placeholder is likely expected to be combined with something like the child records loop in the snippets area. It’ll create a similar sort of wrapper code that makes sense with the indexing as you can have many linked records.In the left area below the field listing there will be a snippet listing with one of them named “child records loop”, inserting that will give you a snippet of code that goes with the indexed line above and it might help make sense of things.
Keep in mind that the relationship you have includes multiple linked records, so you need to determine which one of the records you actually want if there is more than one (if there is only one then that is generally a little easier).
Do you mind giving the child records loop snippet a shot and see if that helps fill in the gaps?
January 3, 2020 at 7:01 PM #39085
BrendanKeymasterHi Sonya,
As Sam suggested, you should try the Child Records Loop.
First, select your
framecost
field in the Fields list on the left.Then double-click on the
Child Records Loop
Snippet.Tap Forms will write the code for you that will loop through all of the child records of your
PortfolioPaintings
field.But if you think you’ll only ever have 1 record in your
PortfolioPaintings
Link to Form field, then you can use:var framecost = portfoliopaintings[0].getFieldValue('fld-c9071aa3421e4715a6bda880b63a32a3');
Because arrays are 0 indexed, the first element of the array starts at position 0.
January 4, 2020 at 6:51 AM #39086
Sonya WaltersParticipantHi, Sam, And Brendan,
Oh, I see. I’ll try both methods. It will probably take me a while.
Thank you so much.Sonya
January 4, 2020 at 11:48 AM #39087
Sonya WaltersParticipantHi, its Sonya again,
It worked! Because I only ever have one record in the PortfolioPaintings link to form field, I tried the non-Child Loop method:
//-----------Pulls field from one-record linked form----------- function FramePriceFromPortfolioDB() { //Get id of record from current form var portfoliopaintings = record.getFieldValue('fld-0b51acdae23a47789d7210379a6ddf2f'); //Using the id, pull the one record in the linked form and find a field in that form // (since there is only one record in the linked form, the index value of zero works). var frameprice = portfoliopaintings[0].getFieldValue('fld-c9071aa3421e4715a6bda880b63a32a3'); return frameprice; } FramePriceFromPortfolioDB(); //. --- [ end script to pull linked form field]
I went on to the Child Loop method but I’m getting an error with the “length” variable…
ScriptWithChildLoop: TypeError: undefined is not an object (evaluating 'portfoliopaintings.length'), column:48, line:12
Here’s the script, sorry for all the code:
//---- Child Loop method ----- //Pulls the framecost field from linked portfoliopaintings form function recordsLoop() { var portfoliopaintings_id = record.getFieldValue('fld-fdfa6263cd8c49ceb0bb048a841ab469'); var framecost_id = 'fld-c9071aa3421e4715a6bda880b63a32a3'; var portfoliopaintings = record.getFieldValue(portfoliopaintings_id); for (var index = 0, count = portfoliopaintings.length; index < count; index++){ var framecost = portfoliopaintings[index].getFieldValue(framecost_id); if (framecost) { return framecost; } } return; } recordsLoop(); ---- [end code with Child Loop method] -----
I think I’m all set since the first method works.
This is a fantastic database and software, thank you for your support.Sonya
January 4, 2020 at 2:13 PM #39089
BrendanKeymasterHi Sonya,
When you post code, remember to put tilde characters before and after the code. You can also select some text and then click on the CODE button in the toolbar at the top of this editor screen.
But your issue with the child records loop version is because instead of providing an ID for your portfoliopaintings_id variable, you’re providing a value from the field by using the
getFieldValue()
function.Instead of:
var portfoliopaintings_id = record.getFieldValue('fld-fdfa6263cd8c49ceb0bb048a841ab469');
You should use:
var portfoliopaintings_id = 'fld-fdfa6263cd8c49ceb0bb048a841ab469';
Thanks,
Brendan
-
AuthorPosts
You must be logged in to reply to this topic.