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.