Accessing record from inside a table field?

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Accessing record from inside a table field?

Viewing 12 reply threads
  • Author
    Posts
  • April 11, 2022 at 1:44 AM #47073

    Anonymous
    Inactive

    Hello everybody,

    I like to access the record of a Form F that has a table field T from inside a script field inside T. If I use record.values I get a dictionary of the form

    {"Id of table field 1" = value of table field 1; 
    "Id of table field 2" = value of table field 2; …  }

    .

    But I need to access the value of a specific field of F from inside the script field of the table field T, i.e I need a dictionary of the form

    {"Id of field 1 of F": value of field 1 of F;
    "Id of field 2 of F": value of field 2 of F; …}

    .

    Is this possible?

    Cheers

    April 11, 2022 at 8:05 AM #47074

    Daniel Leu
    Participant

    Try using record.getFieldValue(table_field_id). This will return an array of records.

    April 11, 2022 at 9:49 AM #47075

    Anonymous
    Inactive

    Thank you.

    The table has 4 columns, Set, Reps, Weight and Target Weight and the Id of the table is fld-f59e078819e24d2aa1492d36d8fe268c.

    The situation is the following:

    1. The form contains a field F that is not a table and a field that is the table T.

    2. The table T contains a column T_s of type script.

    3. For any given record I want to have access to the value of field F inside the script that pertains to T_s.

    If I run the following code inside T_s’ script (fld-f59e078819e24d2aa1492d36d8fe268c is the Id of the table)

    
    var table_field_id = 'fld-f59e078819e24d2aa1492d36d8fe268c';
    
    var testrecords = record.getFieldValue(table_field_id);
    	
    console.log(testrecords.length);
    		
    var testvalues = record.values;
    		
    console.log(testvalues.length);	
    

    I get the output:

    
    11.04.22, 18:44:59 / Sets Old / Target Weight
    0
    undefined
    

    I’am new to Tap Forms 5. Maybe missing some fundamental stuff here?

    Cheers

    April 11, 2022 at 10:19 AM #47076

    Daniel Leu
    Participant

    Do you mind to share your form template? That might make it a bit easier to help.

    April 11, 2022 at 10:51 AM #47077

    Anonymous
    Inactive

    Sure. Thank you.

    April 11, 2022 at 4:23 PM #47079

    Daniel Leu
    Participant

    Thank you for sharing your form template. When you use record in a field script inside a table, you are referring to that row in the table and not the overall record anymore.

    I don’t think that there is a way to get to the main record from the table field script. How about moving the intensity calculation to the main record and update the table’s intensity field from there?

    April 11, 2022 at 6:12 PM #47080

    Anonymous
    Inactive

    Dear Daniel,

    the idea to move the intensity calculation to the main record was really helpful.
    Thank you for your kind help. I greatly appreciate it.

    Cheers.

    April 11, 2022 at 11:53 PM #47091

    Brendan
    Keymaster

    Ok, well this kind of works, but it’s not great:

    function Script() {
    	var first_name = record.getFieldValue('fld-b955dffe477c494db26f60faf1dd9a75');
    
    	var parent_field_id = 'fld-208116bc2576460da36cee99b12e3c04';
    	
    	var parent_field_value = record.parentRecord.getFieldValue(parent_field_id);
    	
    	return parent_field_value;
    
    }
    
    Script();

    In this form I have a Table field with 3 fields in it. A First Name field and a Last Name field and a Script field.

    The first line to get the field value from the Table field’s record is only there to trigger the script to execute when the First Name field is modified. It’s not doing anything with the value.

    The trick is record.parentRecord.getFieldValue(parent_field_id);

    But Tap Forms won’t trigger the Script field to execute if you modify the parent form’s field. I think that’s probably why I didn’t document this. The parent form would have to loop through all Table fields looking for scripts that reference the parent form’s field and then execute the scripts. Just hadn’t really flushed the coding out for that I guess.

    April 12, 2022 at 12:48 AM #47098

    Anonymous
    Inactive

    Thank you Brendan. In case anybody is interested to alter the column of a table field (‘intensity’ in the example) in a form according to some other field in the form:

    Solution according to Daniel Leu:

    Define a (hidden) script field in the form:

    
    function Intensity_Calculation() {
    	var onerm = record.getFieldValue('fld-22846df6ecc24d0d8c4cf9dfbb7ea75a');
    	var table = record.getFieldValue('fld-f59e078819e24d2aa1492d36d8fe268c');
    	
    	for (var index = 0, count = table.length; index < count; index++){
    		var intensity = table[index].getFieldValue(
                   'fld-d12f0981ef2c426988233a09cf0d9bb0')/onerm;
    
    		table[index].setFieldValue('fld-fb5ff2183ad24d62a4a11ced1922b6ea', intensity);
    	}
    }
    
    Intensity_Calculation();
    

    Solution according to Brendan via a script inside the table field:

    
    function Intensity_2() {
    
    	// Getting the field value from the field 'Weight' in the table, 
            // is used in the following calculation and triggers
    	// the script to execute when the field 'Weight' is modified.
    	
            var weight = record.getFieldValue('fld-d12f0981ef2c426988233a09cf0d9bb0');
    	
    	// The id of the field 'OneRm' of the parent form.
    	var parent_one_rm_id = 'fld-22846df6ecc24d0d8c4cf9dfbb7ea75a';
    	
    	// Getting the value of field 'OneRm'	
    	var parent_one_rm_id = record.parentRecord.getFieldValue(parent_one_rm_id);
    		console.log(parent_one_rm_id);
           
           	// A simple calculation		
    	var intensity_2 = weight/parent_one_rm_id;	
    
    	return intensity_2;
    
    }
    
    Intensity_2();
    

    See also the attached form template.

    Cheers.

    April 12, 2022 at 8:46 AM #47102

    Daniel Leu
    Participant

    Great that you got it working.

    At the end of your function, there should be a document.saveAllChanges().

    April 13, 2022 at 2:58 PM #47104

    Anonymous
    Inactive

    Thank you!

    April 15, 2022 at 12:34 AM #47117

    Brendan
    Keymaster

    Oops. It would seem that I accidentally deleted this user who posted this topic when I was clearing out spam users. Doh! Sorry about that if you ever come back.

    April 17, 2022 at 1:47 PM #47124

    Goutalco
    Participant

    I notice that, :-)

Viewing 12 reply threads

You must be logged in to reply to this topic.