Unable to Auto-populate User Select Field

Question / Problem

I am trying to auto-populate a User Selection field on my app with the logged in employee's supervisor. I believe I'm getting all the correct data and setting the value correctly, but nothing is displayed on the user selection field.

Current Situation

I have a user selection custom field on my users' profiles, which designates their supervisors. I'm building a PTO approval app with a user selection field, which I want to be auto-populated with that supervisor immediately on new record creation.

I was able to retrieve the custom field from the logged-in user via a GET user API call, and then I got the supervisor's user data to create an object with code: and name: as keys. (I observed this format from other User Select fields). I then assigned the User Select field the value of that object.

I can see the correct value has been assigned that value if I console log the app.record.create.show event, however it isn't reflected on the user selection field. It just stays blank, and if I try to save the record it throws an error because it is a required field.

Code / Attempts

This is the relevant code:

    // Code snippet
kintone.events.on('app.record.create.show', function(event) {
    var loginUser = kintone.getLoginUser();
    var userID = loginUser.id
    var supervisor_field = 'User_selection';
    var userBody = {
        'ids': userID
    };

    // Retrieve full user data including custom fields, where supervisor info is stored
    kintone.api(kintone.api.url('/v1/users.json', true), 'GET', userBody, function(resp) {
        // success
        var supervisorID = resp.users[0].customItemValues[0].value
        var supervisorBody = {
            'ids': supervisorID
        };
        // Retrieve full user info for supervisor in order to grab code and name fields (login name, unique)
        kintone.api(kintone.api.url('/v1/users.json', true), 'GET', supervisorBody, function(resp2) {
            // success
            var supervisorCode = resp2.users[0].code
            var supervisorName = resp2.users[0].name
            var supervisorObject = [{code: supervisorCode, name: supervisorName}]
            var record = event.record;
            record[supervisor_field].value = supervisorObject;
        }, function(error) {
            // error
            console.log(error);
        });
    }, function(error) {
        // error
        console.log(error);
    });
    return event;

I've been trying to figure this out for hours now, any help would be greatly appreciated! Thank you.

Just FYI if anyone else runs into this, I finally figured out how to solve it using "await"! Changed the API calls to the following format within "try {}", like so:

try {
   let resp = await kintone.api(kintone.api.url('/v1/users.json', true), 'GET', userBody);
// Rest of variables/code
} catch (error) {
console.log(error);
}
2 Likes

Hello @DanP

I noticed you already figured it out yourself, but I will post this reply.
Upon examining the code snippet you provided, I noticed that it involves a process to set a value in the "User Selection" field within the callback function of the kintone.api call.
However, the current implementation may not be effective in setting or retrieving record values within this callback function.

For the correct usage of functions to retrieve or set record values within the kintone.api callback, please refer to the following resources:

The issue with setting values in the "User Selection" field likely stems from the absence of these prescribed processes in your code.

Additionally, by adjusting the code around the setting of the supervisorObject variable as demonstrated below, you should be able to successfully set a value in the "User Selection" field:

var supervisorObject = [{code: supervisorCode, name: supervisorName}]
// var record = event.record;
// Get Record Data
var wRecord = kintone.app.record.get();
wRecord.record[supervisor_field].value = supervisorObject;
// Set Record Data
kintone.app.record.get(wRecord);

Please implement this modified approach in your environment and test it to see if it resolves the issue with the "User Selection" field.

1 Like