Populate values into table fields from dropdown selection

Hello Everyone,

I have a table in my Kintone app that has the following fields:

Table Name = TableA
DROPDOWN = ExistingBC
TEXT = Color
TEXT = Top
TEXT = Description

What I'm trying to accomplish is when I select a value from the DROPDOWN, for example, I select 50001. I want to be able to populate the

Text.Color = "Black"
Text.Top = "Solid"
Text.Description = "Black Plastic Pail with Solid Top"

I get this to work great when I am populating fields that are not in a table. I was using a Switch (Case, Break) code with each BC number to populate the individual fields, but when I try to apply it to a table with the same fields in the table, it will not populate, so I'm thinking I'm missing a step or 2 when it comes to tables. Any thoughts, tips, or suggestions would be very appreciative. Thank you.

(function () {
  "use strict";

  var DRUMBCNUM = "drop_bc_num";
  var DRUMCOLOR = "drum_bc_exist_color";
  var DRUMTOP = "drum_bc_exist_top";
  var DRUMCOM = "drum_bc_description";

  var dropdownEvents = [
    "app.record.edit.change." + DRUMBCNUM,
    "app.record.create.change." + DRUMBCNUM,
  ];

  kintone.events.on(dropdownEvents, function (event) {
    var record = event.record;
    var dbcnum = record[DRUMBCNUM].value;

    switch (dbcnum) {
      case "50001":
        record[DRUMCOLOR].value = "Black";
        record[DRUMTOP].value = "Closed - Lined";
        record[DRUMCOM].value = "N-3 OS Tight Head Drums";
        break;
      case "50002":
        record[DRUMCOLOR].value = "Black";
        record[DRUMTOP].value = "Open - Unlined";
        record[DRUMCOM].value = "N-2 Open Head Unlined Black";
        break;
      case "50011":
        record[DRUMCOLOR].value = "Black";
        record[DRUMTOP].value = "Open - Lined";
        record[DRUMCOM].value = "N-4  Open Head (Lined ) Drums";
        break;
      case "50023":
        record[DRUMCOLOR].value = "Blue";
        record[DRUMTOP].value = "Closed - Lined";
        record[DRUMCOM].value = "N-9 OS -Tight Head Drums - Blue";
        break;
      case "50026":
        record[DRUMCOLOR].value = "Red";
        record[DRUMTOP].value = "Closed - Lined";
        record[DRUMCOM].value = "Tight Head Drums - Orange ENEOS Brand";
        break;
      default:
        record[DRUMCOLOR].value = "";
        record[DRUMTOP].value = "";
        record[DRUMCOM].value = "";
    }
    return event;
  });
})();

Hello @dpoetsch,

Your script does not account for the fields being inside a table. The script assumes the fields are directly part of the record and not inside table rows. Here is the corrected script:

(function() {
    'use strict';

    var DRUMBCNUM = 'drop_bc_num';
    var DRUMCOLOR = 'drum_bc_exist_color';
    var DRUMTOP = 'drum_bc_exist_top';
    var DRUMCOM = 'drum_bc_description';

    var dropdownEvents = [
        'app.record.edit.change.' + DRUMBCNUM,
        'app.record.create.change.' + DRUMBCNUM,
    ];

    kintone.events.on(dropdownEvents, function(event) {
        var record = event.record;
        var tableRows = record.Table.value; // Replace 'Table' with your actual table field code

        for (var i = 0; i < tableRows.length; i++) {
            var row = tableRows[i].value;
            var dbcnum = row[DRUMBCNUM].value;

            switch (dbcnum) {
                case '50001':
                    row[DRUMCOLOR].value = "Black";
                    row[DRUMTOP].value = "Closed - Lined";
                    row[DRUMCOM].value = "N-3 OS Tight Head Drums";            
                    break;
                case '50002':
                    row[DRUMCOLOR].value = "Black";
                    row[DRUMTOP].value = "Open - Unlined";
                    row[DRUMCOM].value = "N-2 Open Head Unlined Black";
                    break;
                case '50011':
                    row[DRUMCOLOR].value = "Black";
                    row[DRUMTOP].value = "Open - Lined";
                    row[DRUMCOM].value = "N-4 Open Head (Lined) Drums";
                    break;
                case '50023':
                    row[DRUMCOLOR].value = "Blue";
                    row[DRUMTOP].value = "Closed - Lined";
                    row[DRUMCOM].value = "N-9 OS -Tight Head Drums - Blue";
                    break;
                case '50026':
                    row[DRUMCOLOR].value = "Red";
                    row[DRUMTOP].value = "Closed - Lined";
                    row[DRUMCOM].value = "Tight Head Drums - Orange ENEOS Brand";
                    break;
                default:
                    row[DRUMCOLOR].value = "";
                    row[DRUMTOP].value = "";
                    row[DRUMCOM].value = "";
            }
        }

        return event;
    });

})();

Hi @Chris,

Thank you for your time and knowledge. I knew I was missing something related to having a table in the equation. I will give this a shot. I believe this is the second time helping me out with a table question. Thank you again!

2 Likes