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.