0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【kintone】計算式の循環参照エラー対策

Last updated at Posted at 2025-07-29

計算式の設定時に「フィールドの参照が循環しています」のエラーが出た際の対処例。

無題.png
上図のような場合は、売上割合総売上高 を数値フィールドに置き換えて
以下のJavaScriptを適用すると、エラーを出さずに計算できる。

(() => {
  'use strict';

  const calcEvents = [
    'app.record.create.show',
    'app.record.edit.show',
    'app.record.create.change.売上',
    'app.record.edit.change.売上',
    'app.record.create.change.品別売上表',
    'app.record.edit.change.品別売上表'
  ];

  kintone.events.on(calcEvents, (event) => {
    const record = event.record;

    // 品別売上表の売上を合計して総売上高を求める
    const table = record.品別売上表?.value || [];
    const total = table.reduce((sum, row) => {
      const sales = Number(row.value.売上.value) || 0;
      return sum + sales;
    }, 0);
    record.総売上高.value = total;
    record.総売上高.disabled = true; // 総売上高を編集不可にする

    // 品別売上表の売上割合を 売上 ÷ 総売上高 × 100 で算出
    table.forEach((row) => {
      const sales = Number(row.value.売上.value) || 0;
      const percentage = total > 0 ? (sales / total) * 100 : 0;
      row.value.売上割合.value = Math.round(percentage * 100) / 100;
      row.value.売上割合.disabled = true;
    });

    return event;
  });
})();

タイトルなし.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?