How to display most recent value from a subtable in another field (not in the subtable)

I have a subtable tracking numeric values. In a separate field in the form (NOT in the subtable) I want to display the most RECENT numeric value that's been added to the table. Is there a simple way to do this?

Hello @hjohnson

You might already be aware that the functionality you're seeking isn't available through Kintone's native features. However, it is possible to accomplish this with a custom script.
Here's a basic outline of the steps you'll need to follow:

  1. Determine the Trigger Event:
    Decide when you want this script to execute. This could be upon saving the record, for instance.
    Events - Kintone Developer Program

  2. Fetch Data from the Subtable:
    Retrieve the data you need from the specified subtable in your app.
    Get Record - Kintone Developer Program

  3. Identify the Most Recent Entry:
    Determine which entry in the subtable is the most recent.

  4. Update a Separate Field:
    Use the data from the most recent subtable entry to update a separate field in your record.

For example, if you want the text from the last row of a subtable "Table" field named "Text" to be automatically copied to a separate text field named "Separate" each time a record is saved, you would use a script like the one provided below.

(function() {
    "use strict";

    var updateLatestField = function(event) {
        var record = event.record;
        var subtable = record['Table'].value;

        if (subtable.length > 0) {
            var lastRow = subtable[subtable.length - 1];
            var mostRecentValue = lastRow.value['Text'];

            if (typeof mostRecentValue === 'object' && mostRecentValue !== null) {
                mostRecentValue = mostRecentValue.value;
            }

            record['Latest'].value = mostRecentValue;
        }

        return event;
    };

    kintone.events.on(['app.record.create.submit', 'app.record.edit.submit'], updateLatestField);
})();

Please use the example as your reference.

1 Like

Thank you! I will try this and test. Thank you for the guidance.