Auto Date population

Hello,

Is it possible to have a date field auto-populate when ’ Yes’ is selected in a dropdown field? I’m looking to have the date field read-only and the populated date remain static.

Hello Dontae,
In your case the following events should be used:

app.record.detail.show
app.record.create.show
app.record.create.change.(Dropdown Field Code)
app.record.edit.show
app.record.edit.change.(Dropdown Field Code)

Kintone JavaScript API - Kintone Developer Program

And since you want to auto-populate when ‘Yes’ is selected in a dropdown field, if/else statement should be used. The following is an example script:

(function () {
  "use strict";

  var events = ['app.record.detail.show',
    'app.record.create.show',
    'app.record.create.change.(Dropdown Field Code)',
    'app.record.edit.show',
    'app.record.edit.change.(Dropdown Field Code)'];

  kintone.events.on(events, function (event) {

    var record = event.record;

    if (record['(Dropdown Field Code)']['value'] === 'Yes') {
      //Input Date
    } else {
      //Input Empty Date
    }

  });
})();

Also, the following page explains about how to make a field read-only (or disabling it):

Record Edit Event - Enable/Disable field edits

I hope these helps!

Hello Yuzo,

I appreciate the provided information. I have two follow-up questions.

Field codes are RequestCompleted (this is the dropdown field ) and RequestCompletedDate (this is the date field I would like to auto-populate).

  1. Is the script formatted correctly?
  2. Where do I populate the RequestCompletedDate field code in the script?

********************************************************************************************

(function() {
“use strict”;

var events = [‘app.record.detail.show’,
‘app.record.create.show’,
‘app.record.create.change.(RequestCompleted)’,
‘app.record.edit.show’,
‘app.record.edit.change.(RequestCompleted)’];

kintone.events.on(events, function(event) {

var record = event.record;

//Field cannot be edited
record[‘RequestCompletedDate’].disabled = true;

if (record[’(RequestCompleted)’][‘value’] === ‘Yes’) {
//Input Date
}else {
//Input Empty Date
}

});
})();

 

Hi Dontae,

 

You can set the value in the format YYYY-MM-DD in the “Input Date” part of the “RequestCompletedDate” field.

ex) record['RequestCompletedDate'].value = "2021-05-01";

Also, the “Input Empty Date” part can be emptied by assigning null to the “RequestCompletedDate” field.

ex) record['RequestCompletedDate'].value = null;

If a value is changed in an event, it can be reflected by returning the event object.

ex) return event;

Please take a look at the following help page for more details for this matter;

Field Types:
https://developer.kintone.io/hc/en-us/articles/212494818-Field-Types

Record Create Event>Overwrite field values:
https://developer.kintone.io/hc/en-us/articles/213149077/

Record Edit Event>Overwrite field values:
https://developer.kintone.io/hc/en-us/articles/213149017/

Also, ‘app.record.detail.show’ (an event after the record detail screen is displayed) can not be overwritten even if you return the event object.

Record Details Event:
https://developer.kintone.io/hc/en-us/articles/213149167-Record-Details-Event

Available event object actions do not contain any process to rewrite the value.

For the date setting, if you want to set today’s date, etc., you can use the Date function, or the following page may be helpful to you; please take a look.

Luxon:
https://moment.github.io/luxon/

Hopefully, this helps.