# Tables Data Summary

**URL:** <https://forum.kintone.dev/t/tables-data-summary/986>\
**Category:** General\
**Created:** [May 5, 2024, 6:04am UTC](https://forum.kintone.dev/t/tables-data-summary/986 "2024-05-05T06:04:11Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Hoo](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/alex_hoo/32/578_2.png) [@Alex\_Hoo](https://forum.kintone.dev/u/Alex_Hoo)\
**Post date:** [May 5, 2024, 6:04am UTC](https://forum.kintone.dev/t/tables-data-summary/986/1 "2024-05-05T06:04:11Z")

</div>

Hi, I would like to find out this there anyway to auto summaries the data from table 1 to table 2. It mean when I key in all the items into table 1 and then it will auto summaries in table 2 total qty in each item.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/kintone/original/1X/32784289ccbe2cf001f2ff4cb55d01ea80e6ee7c.png)

---

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/chris/32/44_2.png) [@Chris](https://forum.kintone.dev/u/Chris)\
**Post date:** [May 6, 2024, 4:16pm UTC](https://forum.kintone.dev/t/tables-data-summary/986/2 "2024-05-06T16:16:40Z")

</div>

Hello @Alex_Hoo

Welcome to the community! 😄

Just to clarify, does the summary need to be presented in another table? If the goal is simply to summarize the quantity for each item, it might be simpler to have number fields outside the table for each item and automatically calculate the sum of each item's quantity there.

Let me know your thoughts on this approach.

---

<div class="post-metadata">

**Author:** ![Alex\_Hoo](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/alex_hoo/32/578_2.png) [@Alex\_Hoo](https://forum.kintone.dev/u/Alex_Hoo)\
**Post date:** [May 7, 2024, 12:06am UTC](https://forum.kintone.dev/t/tables-data-summary/986/3 "2024-05-07T00:06:55Z")

</div>

> [@Chris](#):
>
> Just to clarify, does the summary need to be presented in another table? If the goal is simply to summarize the quantity for each item, it might be simpler to have number fields outside the table for each item and automatically calculate the sum of each item's quantity there.

Hi Chris,

Thanks for your response, at first in my application creation I need to summarize and sum up each item into another table for some other purpose. But if the simple way to do it by summarize with sum each item into the same app with sum up number fields outside the table I'm ok also. Thanks and appreciated your help.

Best Regards  
Alex

---

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/chris/32/44_2.png) [@Chris](https://forum.kintone.dev/u/Chris)\
**Post date:** [May 7, 2024, 12:42am UTC](https://forum.kintone.dev/t/tables-data-summary/986/4 "2024-05-07T00:42:50Z")

</div>

Hello @Alex_Hoo

Understood.  
Yeah, a scripting involving modifying a table, such as adding or deleting rows, can get complex. I'll work on the script that will sum up number fields outside the table and get back to you with it tomorrow.

---

<div class="post-metadata">

**Author:** ![Alex\_Hoo](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/alex_hoo/32/578_2.png) [@Alex\_Hoo](https://forum.kintone.dev/u/Alex_Hoo)\
**Post date:** [May 7, 2024, 12:47am UTC](https://forum.kintone.dev/t/tables-data-summary/986/5 "2024-05-07T00:47:41Z")

</div>

Hi Chris,

Really thanks and appreciated your help.

Best Regards  
Alex

---

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/chris/32/44_2.png) [@Chris](https://forum.kintone.dev/u/Chris)\
**Post date:** [May 7, 2024, 3:56pm UTC](https://forum.kintone.dev/t/tables-data-summary/986/6 "2024-05-07T15:56:08Z")

</div>

Hello @Alex_Hoo

The following script would do the job:

```javascript
(function() {
    'use strict';

    var SUBTABLE_CODE = 'Table'; // Subtable field code
    var DROPDOWN_FIELD = 'Item'; // Dropdown field code within the subtable
    var QUANTITY_FIELD = 'Qty'; // Quantity field code within the subtable

    // Define outside field codes
    var OUTSIDE_FIELDS = {
        'Item 1': 'Item1',
        'Item 2': 'Item2',
        'Item 3': 'Item3'
    };

    // Listen for changes in both the Item and Qty fields within the subtable
    var itemChangeEvents = [
        'app.record.edit.change.' + DROPDOWN_FIELD,
        'app.record.create.change.' + DROPDOWN_FIELD
    ];

    var qtyChangeEvents = [
        'app.record.edit.change.' + QUANTITY_FIELD,
        'app.record.create.change.' + QUANTITY_FIELD
    ];

    kintone.events.on(itemChangeEvents.concat(qtyChangeEvents), function(event) {
        var record = event.record;
        var aggregates = {
            'Item 1': 0,
            'Item 2': 0,
            'Item 3': 0
        };

        // Aggregate quantities for each item type
        record[SUBTABLE_CODE].value.forEach(function(row) {
            var item = row.value[DROPDOWN_FIELD].value;
            var qty = parseFloat(row.value[QUANTITY_FIELD].value) || 0;
            if (item in aggregates) {
                aggregates[item] += qty;
            }
        });

        // Update outside fields based on the aggregates
        for (var item in aggregates) {
            var fieldCode = OUTSIDE_FIELDS[item];
            record[fieldCode].value = aggregates[item];
        }

        return event;
    });

    // Disable the outside number fields on form show events
    var formShowEvents = [
        'app.record.create.show',
        'app.record.edit.show',
        'app.record.detail.show'
    ];

    kintone.events.on(formShowEvents, function(event) {
        var record = event.record;
        // Disable each field
        record['Item1'].disabled = true;
        record['Item2'].disabled = true;
        record['Item3'].disabled = true;

        return event;
    });
})();

```

 ![20240508](https://canada1.discourse-cdn.com/flex035/uploads/kintone/original/1X/a4d191f1d6b043a5f2be656347acd061b7b748a1.png)

Here is the information about the fields used:

- Table - Field Code: Table
- Item - Dropdown Field, Field Code: Item
- Qty - Number field, Field Code: Qty
- Item 1 - Number field, Field Code: Item1
- Item 2 - Number field, Field Code: Item2
- Item 3 - Number field, FIeld Code: Item3

---

<div class="post-metadata">

**Author:** ![Alex\_Hoo](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/alex_hoo/32/578_2.png) [@Alex\_Hoo](https://forum.kintone.dev/u/Alex_Hoo)\
**Post date:** [May 8, 2024, 12:02am UTC](https://forum.kintone.dev/t/tables-data-summary/986/7 "2024-05-08T00:02:23Z")

</div>

Hi Chris,

Thanks for your coding, I will try it.

Best Regards  
Alex

---

<div class="post-metadata">

**Author:** ![Alex\_Hoo](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/alex_hoo/32/578_2.png) [@Alex\_Hoo](https://forum.kintone.dev/u/Alex_Hoo)\
**Post date:** [May 8, 2024, 12:23am UTC](https://forum.kintone.dev/t/tables-data-summary/986/8 "2024-05-08T00:23:32Z")

</div>

> [@Chris](#):
>
> Qty

Hi Chris,

Sorry to disturb you again. I just tried your coding and the field required according to the coding. But it does not work after I click save. I'm not sure whether I missed anything.

![image](https://canada1.discourse-cdn.com/flex035/uploads/kintone/original/1X/44c401c5a60fef172b5d51e99f7d0561561115f2.png)

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/kintone/original/1X/e76e77c89bde6bb3cfec626f69394f637dc59a0d.png)

```javascript
(function() {
    'use strict';

    var SUBTABLE_CODE = 'Table'; // Subtable field code
    var DROPDOWN_FIELD = 'Item'; // Dropdown field code within the subtable
    var QUANTITY_FIELD = 'Qty'; // Quantity field code within the subtable

    // Define outside field codes
    var OUTSIDE_FIELDS = {
        'Item 1': 'Item1',
        'Item 2': 'Item2',
        'Item 3': 'Item3'
    };

    // Listen for changes in both the Item and Qty fields within the subtable
    var itemChangeEvents = [
        'app.record.edit.change.' + DROPDOWN_FIELD,
        'app.record.create.change.' + DROPDOWN_FIELD
    ];

    var qtyChangeEvents = [
        'app.record.edit.change.' + QUANTITY_FIELD,
        'app.record.create.change.' + QUANTITY_FIELD
    ];

    kintone.events.on(itemChangeEvents.concat(qtyChangeEvents), function(event) {
        var record = event.record;
        var aggregates = {
            'Item 1': 0,
            'Item 2': 0,
            'Item 3': 0
        };

        // Aggregate quantities for each item type
        record[SUBTABLE_CODE].value.forEach(function(row) {
            var item = row.value[DROPDOWN_FIELD].value;
            var qty = parseFloat(row.value[QUANTITY_FIELD].value) || 0;
            if (item in aggregates) {
                aggregates[item] += qty;
            }
        });

        // Update outside fields based on the aggregates
        for (var item in aggregates) {
            var fieldCode = OUTSIDE_FIELDS[item];
            record[fieldCode].value = aggregates[item];
        }

        return event;
    });

    // Disable the outside number fields on form show events
    var formShowEvents = [
        'app.record.create.show',
        'app.record.edit.show',
        'app.record.detail.show'
    ];

    kintone.events.on(formShowEvents, function(event) {
        var record = event.record;
        // Disable each field
        record['Item1'].disabled = true;
        record['Item2'].disabled = true;
        record['Item3'].disabled = true;

        return event;
    });
})();

```

Thanks & Regards,  
Alex

---

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/chris/32/44_2.png) [@Chris](https://forum.kintone.dev/u/Chris)\
**Post date:** [May 8, 2024, 3:17pm UTC](https://forum.kintone.dev/t/tables-data-summary/986/9 "2024-05-08T15:17:30Z")

</div>

Hello @Alex_Hoo  
Could you share any error messages you're encountering?  
Otherwise, I can't provide much assistance.

Also, please make use of this page to debug the error.

> **[Debug with Developer Tools](https://kintone.dev/en/tutorials/development-productivity/debug-with-developer-tools/)**
>
> This article introduces how to use the Developer Tools to debug Kintone customizations.

---

<div class="post-metadata">

**Author:** ![Alex\_Hoo](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/alex_hoo/32/578_2.png) [@Alex\_Hoo](https://forum.kintone.dev/u/Alex_Hoo)\
**Post date:** [May 9, 2024, 12:24am UTC](https://forum.kintone.dev/t/tables-data-summary/986/10 "2024-05-09T00:24:38Z")

</div>

Hi Chris, thanks for your response. I will try to debug and find out the error and get back to you. Thanks

---

<div class="post-metadata">

**Author:** ![Alex\_Hoo](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/alex_hoo/32/578_2.png) [@Alex\_Hoo](https://forum.kintone.dev/u/Alex_Hoo)\
**Post date:** [May 9, 2024, 12:41am UTC](https://forum.kintone.dev/t/tables-data-summary/986/11 "2024-05-09T00:41:50Z")

</div>

Hi Chris,

I tried debug and I saw this error.

![image](https://canada1.discourse-cdn.com/flex035/uploads/kintone/original/1X/48139188210d996511c4c2f7dee408603035513a.png)

Regards  
Alex

---

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/chris/32/44_2.png) [@Chris](https://forum.kintone.dev/u/Chris)\
**Post date:** [May 9, 2024, 3:41pm UTC](https://forum.kintone.dev/t/tables-data-summary/986/12 "2024-05-09T15:41:04Z")

</div>

Hello @Alex_Hoo  
The error appears to be quite general, making it challenging to identify the specific issue.  
If the structure of your setup mirrors mine, I suspect the error might be due to discrepancies between your field codes and those used in the script.

---

<div class="post-metadata">

**Author:** ![Alex\_Hoo](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/alex_hoo/32/578_2.png) [@Alex\_Hoo](https://forum.kintone.dev/u/Alex_Hoo)\
**Post date:** [May 10, 2024, 12:20am UTC](https://forum.kintone.dev/t/tables-data-summary/986/13 "2024-05-10T00:20:21Z")

</div>

Hi Chris,

Thanks for your information, I will try it again.

Regards  
Alex

---

<div class="post-metadata">

**Author:** ![dpoetsch](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@dpoetsch](https://forum.kintone.dev/u/dpoetsch)\
**Post date:** [September 30, 2024, 8:34pm UTC](https://forum.kintone.dev/t/tables-data-summary/986/14 "2024-09-30T20:34:10Z")

</div>

Hi @Chris,

I was using this code to run something similar and the error I get is

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/kintone/original/1X/df2a16e073a2bce8ea6b75c65960e1f86bed0cc0.png)

It seems to have an issue with line 46 - record[fieldCode].value = aggregates[item];

Below is my code:  
(function() {  
'use strict';

```
var SUBTABLE_CODE = 'aged_inv_fg'; // Subtable field code
var DROPDOWN_FIELD = 'type_fg'; // Dropdown field code within the subtable
var QUANTITY_FIELD = 'inv_value_fg'; // Quantity field code within the subtable

// Define outside field codes
var OUTSIDE_FIELDS = {
    'Total Inventory Value - FG': 'total_num_fg',
    'Total Inventory Value - RM': 'total_num_rm',
    //'Item 3': 'Item3'
};

// Listen for changes in both the Item and Qty fields within the subtable
var itemChangeEvents = [
    'app.record.edit.change.' + DROPDOWN_FIELD,
    'app.record.create.change.' + DROPDOWN_FIELD
];

var qtyChangeEvents = [
    'app.record.edit.change.' + QUANTITY_FIELD,
    'app.record.create.change.' + QUANTITY_FIELD
];

kintone.events.on(itemChangeEvents.concat(qtyChangeEvents), function(event) {
    var record = event.record;
    var aggregates = {
        'NOLA FG': 0,
        'Pass Through': 0
        //'Item 3': 0
    };

    // Aggregate quantities for each item type
    record[SUBTABLE_CODE].value.forEach(function(row) {
        var item = row.value[DROPDOWN_FIELD].value;
        var qty = parseFloat(row.value[QUANTITY_FIELD].value) || 0;
        if (item in aggregates) {
            aggregates[item] += qty;
        }
    });

    // Update outside fields based on the aggregates
    for (var item in aggregates) {
        var fieldCode = OUTSIDE_FIELDS[item];
        record[fieldCode].value = aggregates[item];
    }

    return event;
});

// Disable the outside number fields on form show events
var formShowEvents = [
    'app.record.create.show',
    'app.record.edit.show',
    'app.record.detail.show'
];

kintone.events.on(formShowEvents, function(event) {
    var record = event.record;
    // Disable each field
    record['total_num_fg'].disabled = true;
    record['total_num_rm'].disabled = true;
    //record['Item3'].disabled = true;

    return event;
});

```

})();

This is in my table

![image](https://canada1.discourse-cdn.com/flex035/uploads/kintone/original/1X/d656be990d7f5dd7f889b5570567dccb359fccbd.png)

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/kintone/original/1X/49b950f944b254751795c3e271ba2aaaab97528b.png)

I'm trying to take all the NOLA FG selections and add them together to and put into the Total Inventory Value - FG number field and then do the same with all the "Pass Through" sections, going into the Total Inventory Value - RM number field. So if 2 NOLA FG rows have a combined Inventory value of 100 (25 and 75 in the Qty field) then 100 goes into the Total Inventory Value - FG field and same for the Pass Through. Thank you in advance for any corrections, suggestions.

---

<div class="post-metadata">

**Author:** ![dpoetsch](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@dpoetsch](https://forum.kintone.dev/u/dpoetsch)\
**Post date:** [October 1, 2024, 9:01pm UTC](https://forum.kintone.dev/t/tables-data-summary/986/15 "2024-10-01T21:01:56Z")

</div>

Hey @Chris,

Nevermind I figure it out. I had the right fields just in the wrong spot in the code. I have it working now. Thanks
