# あるフィールドの値を参照し、別のフィールドに自動で値を入力する方法につきまして

**URL:** <https://community.cybozu.dev/t/topic/3049>\
**Category:** kintone 開発相談\
**Created:** [2018 年 5 月 10 日午前 11:15 UTC](https://community.cybozu.dev/t/topic/3049 "2018-05-10T11:15:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account2030](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@Legacy\_Account2030](https://community.cybozu.dev/u/Legacy_Account2030)\
**Post date:** [2018 年 5 月 10 日午前 11:15 UTC](https://community.cybozu.dev/t/topic/3049/1 "2018-05-10T11:15:05Z")

</div>

日付フィールド（注文日、開始日、終了日）の値を判断し、  
文字列フィールド（状態）の値を自動で入力するスクリプトを  
作成したいのですが、うまく動作しません。アドバイスをお願いします。

* * *

## 項目 フィールド名

## 注文日 orderDate 開始日 startDate 終了日 endDate 状態 status

■条件  
＜状態(status)の値＞  
(1)注文日・開始日・終了日・状態 は空（null） → 未発注  
(2)注文日に日付が入っている ＆ 開始日・終了日・状態 は空（null） → 発注済  
(3)注文日・開始日に日付が入力 ＆ 終了日・状態 は空（null） → 対応中  
(4)注文日・開始日・終了日に日付が入力 → 完了

&nbsp;

■参考にしたURL  
\<[https://developer.cybozu.io/hc/ja/community/posts/115009807906-%E3%83%95%E3%82%A3%E3%83%BC%E3%83%AB%E3%83%89%E3%81%AE%E5%80%A4%E3%81%AB%E3%82%88%E3%81%A3%E3%81%A6%E5%88%A5%E3%83%95%E3%82%A3%E3%83%BC%E3%83%AB%E3%83%89%E3%82%92%E8%87%AA%E5%8B%95%E6%9B%B4%E6%96%B0%E3%81%99%E3%82%8B](https://developer.cybozu.io/hc/ja/community/posts/115009807906-%E3%83%95%E3%82%A3%E3%83%BC%E3%83%AB%E3%83%89%E3%81%AE%E5%80%A4%E3%81%AB%E3%82%88%E3%81%A3%E3%81%A6%E5%88%A5%E3%83%95%E3%82%A3%E3%83%BC%E3%83%AB%E3%83%89%E3%82%92%E8%87%AA%E5%8B%95%E6%9B%B4%E6%96%B0%E3%81%99%E3%82%8B)\>

■考えているスクリプト

//----------フィールドの値によって別フィールドを自動更新する(エラーで苦戦中)----------------------------

var events = [‘app.record.index.show’,  
‘app.record.detail.show’,  
‘app.record.create.show’,  
‘app.record.edit.show’,  
‘app.record.index.edit.show’,  
‘app.record.create.change.status’,  
‘app.record.edit.change.status’,  
‘app.record.index.edit.change.status’,  
‘app.record.create.change.endDate’,  
‘app.record.edit.change.endDate’,  
‘app.record.index.edit.change.endDate’,  
‘app.record.create.change.orderDate’,  
‘app.record.edit.change.orderDate’,  
‘app.record.index.edit.change.orderDate’];

kintone.events.on(events, function(event) {  
var record = event.record;  
record[‘status’][‘disabled’] = true;

if (record[‘endDate’].value != null) {  
record[‘status’].value === ‘作業完了’;

} else if (record[‘orderDate’].value != null) {  
record[‘status’].value === ‘発注済’;

} else {  
record[‘status’].value === ‘未発注’;  
}  
return event;  
});

以上、よろしくお願いいたします。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1517](https://avatars.discourse-cdn.com/v4/letter/l/4bbf92/32.png) [@Legacy\_Account1517](https://community.cybozu.dev/u/Legacy_Account1517)\
**Post date:** [2018 年 5 月 10 日午後 11:41 UTC](https://community.cybozu.dev/t/topic/3049/2 "2018-05-10T23:41:02Z")

</div>

Sophia さん

コードを見ると、代入ではなく論理式になっているので値が更新されません。

ただし、論理式を記述してもエラーにはならないと思いますので、別のエラー原因があると思います。

ブラウザーの開発ツールで調査してみてください。

[動かない？そんな時はデバッグをしてみよう！入門編

](/hc/ja/articles/207613916)

項目の入力済み判定ですが、下記が参考になります。

[（小技）undefined と 空文字のスマートな if 文判定](https://community.cybozu.dev/hc/ja/articles/201718170)

あと日付項目をクリアする場合、空文字"" でもクリアできてしまいます。

null, undefined, “” は、未入力として判定したほうがいいと思います。

```
(function() {
"use strict";
var events = ['app.record.index.show',
'app.record.detail.show',
'app.record.create.show',
'app.record.edit.show',
'app.record.index.edit.show',
'app.record.create.change.status',
'app.record.edit.change.status',
'app.record.index.edit.change.status',
'app.record.create.change.endDate',
'app.record.edit.change.endDate',
'app.record.index.edit.change.endDate',
'app.record.create.change.orderDate',
'app.record.edit.change.orderDate',
'app.record.index.edit.change.orderDate'];

kintone.events.on(events, function(event) {
var record = event.record;
record['status']['disabled'] = true;
if (record['endDate'].value) {
record['status'].value = '作業完了';
} else if (record['orderDate'].value) {
record['status'].value = '発注済';
} else {
record['status'].value = '未発注';
}
return event;
});
})();

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2030](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@Legacy\_Account2030](https://community.cybozu.dev/u/Legacy_Account2030)\
**Post date:** [2018 年 5 月 11 日午前 12:20 UTC](https://community.cybozu.dev/t/topic/3049/3 "2018-05-11T00:20:53Z")

</div>

rex0220様

&nbsp;

アドバイスありがとうございます。

私の記載が正しくありませんでした。エラー＝思った通りに動かない との意味で記載しておりました。

&nbsp;

\>代入ではなく論理式になっているので値が更新されません。

&nbsp;

こちらの部分の理解が不足しておりますので、頂きました

サンプルとともに確認を行います。

&nbsp;
