Automatically select department

Is there a way to automatically complete the selection of a Department within a form?  Most of our members are only a member of one Department.  We currently require the user to select their Department within each form.  It seems like an annoyance to have to select the Department within the form when the user only has one Department affiliation.  We would also need an option to change or select another Department if the user is a member of several Departments.

 

 

Hello Tony,

If the department field that you want to auto-fill is for the user that is currently logged in, then you can do this using the default feature.

Every user has a priority department that is set up in the User Administration settings. If a user belongs only to one department, then the priority department will automatically set to that one department.

Then, in the department field settings, you should see a Default Value section.
In the default value, go to the select departments and click on the Others tab. In the Others tab, you will see the Priority Department as a selection so select that and click Add. Finally, save the app.

By doing this, the priority department of the currently logged in user will automatically be selected upon creating a record.

However, with this setting, if the user belongs to multiple departments and the priority department is not the one that the user wants to select, then the user will need to clear the existing department before saving the record, or else multiple departments will be selected if the user selects an additional department.

If you don’t want the user to have multiple departments selected, you might could put some kind of an error in case multiple departments are selected. For this part, you will need to implement a js script.

I found a help page that explains how to limit the number of the selection in a field that requires selecting a choice. 
The page is about the User Selection field, but this can be customized to the department field.

kintone developer network - Allow only 1 user for User Selection fields
https://developer.kintone.io/hc/en-us/articles/115002475233-Allow-only-1-user-for-User-Selection-fields

The following is an example that I borrowed from the example in the above page and arranged it for the department field.

(function() {
'use strict';
var depart_selection = "Department_selection"; //Set the field code of the Department Selection field
var error_message = 'Only one department 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 Department Selection field information
var record = event.record;
var selectedDeparts = record[depart_selection]['value'];
if (selectedDeparts.length > 1) {
// If more than one department is specified, set message to error property
event.error = error_message;
}
return event;
});
})();

I hope this helps.

 

Setting Priority Department worked like a charm.  

Hi Yuzo Arai,

 

Can we assign automatically on user selection field as thier manager. Example when A create record and then manager B will automatically be selected.

 

It’s similar when department be automatically select as user login

 

 

Hello Trithot!

There’s actually an article that perfectly answers your question.
Follow the instructions on the article and just tweak the settings a little to assign the manager.

Kintone Developer Program - Auto-assign a user when adding a record
https://developer.kintone.io/hc/en-us/articles/115008297267–Auto-assign-a-user-when-adding-a-record

I hope this helps!