Created
November 28, 2019 20:16
-
-
Save leo-bianchi/7b20bc6a2013b02cc918d016ce5c2e4f to your computer and use it in GitHub Desktop.
Servicenow catalog item onChange client script to verify duplicates inside multi-row variable set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onChange(control, oldValue, newValue, isLoading) { | |
if (isLoading || newValue == '' || newValue == 'false') { | |
return; | |
} | |
var multiRowSysId = 'sys_id'; | |
var checkBoxField = 'os_horarios_forao_totalmente_preenchidos'; | |
// Mensagem de erro | |
var msg = 'Há mais de uma ocorrência para um mesmo dia, por favor, edite ou remova o registro.'; | |
// Valores do multi-row | |
var multiRow = g_form.getValue(multiRowSysId); | |
if(multiRow) { | |
// multiRow: string -> multiRowObject: object | |
var multiRowObject = JSON.parse(multiRow); | |
// Array com os dias da semana | |
var days = multiRowObject.map(function(multiRowObject) { | |
return multiRowObject.dia_semana; | |
}); | |
/** | |
* @param {Array} x - Dias da semana | |
* @returns {boolean} Retorna true se o tamanho do set for do mesmo tamanho do array informado | |
*/ | |
var hasDuplicate = function(x) { | |
// Set (Array sem duplicatas) | |
return new Set(x).size !== x.length; | |
}; | |
if(hasDuplicate(days)) { | |
g_form.setValue(checkBoxField, false); | |
g_form.addErrorMessage(msg); | |
g_form.showFieldMsg(checkBoxField, msg, 'error'); | |
} | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment