I'm hoping someone can assist me. I have an app that we use for President approvals. I have an Officer selection field (User Selection with preset users) and I have 2 Person of Authority (POA) fields (User Selection with preset users). The officers of the company listed in the Officers selection are also listed in the POA fields. Sometimes we get users that will select Jim Smith in the Officer field and also select Jim Smith in the POA field. I'm trying to write a js so that if that happens an error pops ups and tells the user that they cant do that. Now if I set my if statement to !== the error pops up even if both Jims are selected and if Jim and Dave are selected. I added 2 lines of alert to get a pop up to see if I can tell if the names are correct but all I get is "Object". What am I missing? It doesn't seem to be reading the selected names. Thank you for your time.
My Code is basic and I am newer to writing in js
(function() {
'use strict';
var officer = 'tier2';
var poa1 = 'PIA1';
var poa2 = 'PIA2';
var error_message1 = 'ERROR - The Officer and POA1 are the same. Only one can be specified. Please check your selection';
//var error_message2 = 'ERROR - The Officer and POA2 are the same. Only one can be specified. Please check your selection';
var FE_MESSAGE = 'ERROR - DUPLICATE';
var events = ['app.record.create.submit.success', 'app.record.create.submit',
'app.record.create.change','app.record.edit.change',
'app.record.edit.submit', 'app.record.index.edit.submit'];
kintone.events.on(events, function(event) {
// Get the User Selection field information
var record = event.record;
var selectedOfficer = record[officer].value;
var selectedPOA1 = record[poa1].value;
//var selectedPOA2 = record[poa2].value;
Sorry, this is my js code the code above was me playing around try to get to see the names that it is trying to compare:
(function() {
'use strict';
var officer = 'tier2';
var poa1 = 'PIA1';
var poa2 = 'PIA2';
var error_message1 = 'ERROR - The Officer and POA1 are the same. Only one can be specified. Please check your selection';
var error_message2 = 'ERROR - The Officer and POA2 are the same. Only one can be specified. Please check your selection';
var FE_MESSAGE = 'ERROR - DUPLICATE';
var events = ['app.record.create.submit.success', 'app.record.create.submit',
'app.record.create.change','app.record.edit.change',
'app.record.edit.submit', 'app.record.index.edit.submit'];
kintone.events.on(events, function(event) {
var record = event.record;
var selectedOfficer = record[officer].value;
var selectedPOA1 = record[poa1].value;
var selectedPOA2 = record[poa2].value;
if(selectedOfficer === selectedPOA1) {
event.error = error_message1;
record[poa1].error = FE_MESSAGE;
record[officer].error = FE_MESSAGE;
}
if(selectedOfficer == selectedPOA2) {
event.error = error_message2;
record[poa2].error = FE_MESSAGE;
}
return event;
Hello @dpoetsch
The issue with your script is that the user selection fields (tier2, PIA1) store an array of objects rather than a direct string value. Your script attempts to compare them directly using .value, which doesn’t work correctly since .value returns an array of user objects, not a single string.
Edit: Sorry, I noticed your 2nd comment after I posted here. The revised version is based on your first script, but it still works.
Here’s a revised version:
(function() {
'use strict';
var officer = 'tier2';
var poa1 = 'PIA1';
var error_message1 = 'ERROR - The Officer and POA1 are the same. Only one can be specified. Please check your selection';
var FE_MESSAGE = 'ERROR - DUPLICATE';
var events = [
'app.record.create.submit',
'app.record.edit.submit'
];
kintone.events.on(events, function(event) {
var record = event.record;
// Extract user names safely
var selectedOfficer = Array.isArray(record[officer].value) && record[officer].value.length > 0
? record[officer].value[0].name : "";
var selectedPOA1 = Array.isArray(record[poa1].value) && record[poa1].value.length > 0
? record[poa1].value[0].name : "";
if (selectedOfficer !== "" && selectedOfficer === selectedPOA1) {
event.error = error_message1;
record[poa1].error = FE_MESSAGE;
record[officer].error = FE_MESSAGE;
}
return event;
});
})();
Aw, thank you @Chris. I started to read about Arrays in js and thought that might be the issue. Thanks again. I will put your changes in my code and test it out. Thanks again for your help and knowledge.