0
1

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 フィールド/レイアウト取得が「JS API対応」されたので早速使ってみました。

Last updated at Posted at 2025-11-08

— REST APIからの移行ポイントと実用サンプル

最近、kintoneのfieldsとlayoutがJS APIで直接取得できるようになりました。
APIコール数を減らすためにも、切り替えていきたいですね。

結論

  • Fields:RESTはresp.propertiesだったけど、JSは最初から辞書(= いきなりfields[code]で参照OK)。
  • Layout:RESTはresp.layoutだったけど、JSは最初から配列。

つまり「トップ階層のラッパが消えた」のが最大の差。
ここを直せば、下流ロジックはほぼそのままでした。

1)「フィールド一覧を取得」 kintone.app.getFields();

Before: REST API

// REST: フィールド定義を取得 → propertiesを剥がす
const fieldsResp = await kintone.api(
  kintone.api.url('/k/v1/app/form/fields.json', true),
  'GET',
  { app: kintone.app.getId() }
);
const fields = fieldsResp.properties; // ← ここがポイント
console.log(Object.keys(fields));     // 例: ["レコード番号","顧客名","テーブル",...]

After: JS API

// JS: いきなり辞書で返る(properties不要)
const fields = await kintone.app.getFields();
console.log(Object.keys(fields));

使い方

// 代表的な取り回し(どちらのAPIでも共通の“その先”の書き方)
const list = Object.values(fields).map(f => ({
  code:  f.code,    // 例: "顧客名", "KOKCD", ...
  type:  f.type,    // 例: "SINGLE_LINE_TEXT", "SUBTABLE", ...
  label: f.label,   // 画面表示ラベル
}));
console.table(list);

JSは最初から { code, type, label, ... } の集合として触れるので、fields.propertiesを忘れてundefinedになる事故が消えます。

2) 「レイアウトを抽出」 kintone.app.getLayout();

Before: REST API

const layoutResp = await kintone.api(
  kintone.api.url('/k/v1/app/form/layout.json', true),
  'GET',
  { app: kintone.app.getId() }
);
const layout = layoutResp.layout; // ← RESTはここが必要

After: JS API

const layout = await kintone.app.getLayout(); // ← いきなり配列

3) 既存コードを壊さない“直し方”

  • fieldsResp.propertiesをそのまま残してfields.properties.xxxとしてる
    → 削除してfields.xxxに。
  • layoutResp.layoutをそのまま参照してる
    → 削除してlayoutに。

4) 置換ガイド(最小の手直しで通すなら)

プロジェクト全体検索で一気に直す用のメモです。雑に目安として。

  • something.properties → something
    (ただしSUBTABLE.fieldsは残す!ここは中身の列集合)
  • something.layout → something

まとめ

  • JS API対応で、受け取ってすぐ使える形になりました。
  • 直すのは入口の一手だけでOK。fields.propertiesとlayout.layoutを忘れて、そのまま使うだけ。
  • サブテーブルの扱い方やレイアウトの歩き方は今まで通りで大丈夫。
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?