Limit User Selection Fields to 1 User

Contents

Overview

This article introduces how to limit the User selection field to hold only one value.

By default, User selection fields in Kintone can hold information of multiple users. The number of users that can be selected for the value cannot be controlled with Kintone’s built-in features.

Sample Image

The only field needed for this customization to run is the User selection field.

Animated GIF: User sets 2 users in the User selection field, tries to save the record, but an error is displayed.

Prepare the App

Create an App (External link) with the following fields and settings.

Field Type Field Name Field Code
User selection The person in charge userfield

Sample Code

Prepare the following JavaScript code in a text editor and navigate to the Kintone App’s settings. Upload the file into the Upload JavaScript for PC option of the JavaScript and CSS Customization settings (External link).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
(function() {
  'use strict';
  var user_selection = 'userfield'; // Set the field code of the User Selection field
  var error_message = 'Only one person in charge can be specified.';

  var myEvent = ['app.record.create.submit', 'app.record.edit.submit', 'app.record.index.edit.submit'];
  kintone.events.on(myEvent, function(event) {
    // Get the User Selection field information
    var record = event.record;
    var selectedUsers = record[user_selection].value;
    if (selectedUsers.length > 1) {
      // If more than one person is specified, set message to error property
      event.error = error_message;
    }
    return event;
  });
})();

Navigate to the Record Create page, Record Edit page or the Record List page. Add 2 or more users to the User selection field and click on Save. The record should be unsaved, and an error message should appear.

Animated GIF: User puts 2 users in the User Selection field to, tries to save the record, but an error is displayed.