Verify the radio button's value and show local date time when checked

I am trying to display the local date when the 'Complete' radio button is selected, as shown in the attached file.

Code / Attempts

(function () {
    'use strict';
  
    // Function to update the value of the text field
    function updateTextField(record) {
      var statusValue = record['Status_AF'].value; // Get the value of the radio button field
  
  
      if (statusValue === 'Completed') {
        var currentDatetime = new Date().toLocaleString(); // Get current local datetime
        record['ReplyTest'] = {
          'value': currentDatetime
        };
  
      } else {
        record['ReplyTest'] = {
          'value': ''
        };
      }
    }
  
    // Event listener for changes in the radio button field
    kintone.events.on('app.record.edit.change.Status_AF', function (event) {
      var record = event.record;
  
      // Update the value of the text field
      updateTextField(record);
  
      return event;
    });
  })();

Error Message

Hello @Weerapat_Tangkanakul
It's difficult to discern from the screenshot. Could you please specify the type of field you are using for the Reply field? It doesn't seem like a Date and Time field.

I added the date time field for using it with the radio button it's still the same error.

Hello @Weerapat_Tangkanakul

Thank you for sharing the screenshots.

Based on what you've provided, I've developed a script that automatically captures the current PC time when 'Completed' is selected from a dropdown field. Below is the script tailored to your needs:

(function() {
    'use strict';

    var events = [
        'app.record.create.change.Status_AF',
        'app.record.edit.change.Status_AF'
    ];

    kintone.events.on(events, function(event) {
        var record = event.record;
        var statusValue = record['Status_AF'].value;

        // Debugging
        console.log('Event triggered. Status Value:', statusValue);

        if (statusValue === 'Completed') {
            var now = new Date();
            var isoString = now.toISOString();

            // Debugging
            console.log('Attempting to update Date_and_time to:', isoString);

            record['Date_and_time'].value = isoString;
        }

        // Ensure to return the modified event object
        return event;
    });
})();

If you need any further adjustments, please feel free to edit it.

Thank you.

1 Like