- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2025 04:01 AM
I have a field in the KB creation called category that is a reference to the sc_category table. This field should only display categories that have a title and parent. However, there is a list of categories that do not have a parent but should be displayed. When a new category is created that previously did not have a parent, the previous one should not be displayed. Could someone help me create this script?
Here is the list of categories that do not have a parent and should be displayed:
Tecnologia
Inventário
Descarte e Doação
Internet/Wi-fi
Acessibilidade e Ergonomia
Sinalização e Placas
Contratos e Licitações
QVT Laboral e Massagem
Erro1 Loja Virtual - Dependência Não Localizada
Materiais Expediente, Higiene e Limpeza
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2025 05:51 AM
you can have a reference qualifier on that reference field like this
javascript: 'titleISNOTEMPTY^parentISNOTEMPTY^NQtitleINTecnologia,Inventário,Descarte e Doação,Internet/Wi-fi,Acessibilidade e Ergonomia,Sinalização e Placas,Contratos e Licitações,QVT Laboral e Massagem,Erro1 Loja Virtual - Dependência Não Localizada,Materiais Expediente, Higiene e Limpeza';
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2025 06:15 AM
(function executeRule(current, previous /*null on insert*/) {
// Exception list
var exceptionTitles = [
'Tecnologia',
'Inventário',
'Descarte e Doação',
'Internet/Wi-fi',
'Acessibilidade e Ergonomia',
'Sinalização e Placas',
'Contratos e Licitações',
'QVT Laboral e Massagem',
'Erro1 Loja Virtual - Dependência Não Localizada',
'Materiais Expediente, Higiene e Limpeza'
];
// Check if current title is in exceptions
if (exceptionTitles.indexOf(current.title) == -1) {
return; // Not in exception list, exit
}
// Check if parent changed from empty to something (or if inserted with parent)
var hadNoParentBefore = !previous || !previous.parent;
var hasParentNow = current.parent && current.parent.toString() != '';
if (hadNoParentBefore && hasParentNow) {
// Find old categories with same title but no parent and active=true
var oldCat = new GlideRecord('sc_category');
oldCat.addQuery('title', current.title);
oldCat.addQuery('parent', '');
oldCat.addQuery('active', true); // assuming there is an active field
oldCat.query();
while (oldCat.next()) {
oldCat.active = false; // deactivate old orphan category
oldCat.update();
}
}
})(current, previous);
Reagrds,
Suyash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2025 05:51 AM
you can have a reference qualifier on that reference field like this
javascript: 'titleISNOTEMPTY^parentISNOTEMPTY^NQtitleINTecnologia,Inventário,Descarte e Doação,Internet/Wi-fi,Acessibilidade e Ergonomia,Sinalização e Placas,Contratos e Licitações,QVT Laboral e Massagem,Erro1 Loja Virtual - Dependência Não Localizada,Materiais Expediente, Higiene e Limpeza';
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2025 06:15 AM
(function executeRule(current, previous /*null on insert*/) {
// Exception list
var exceptionTitles = [
'Tecnologia',
'Inventário',
'Descarte e Doação',
'Internet/Wi-fi',
'Acessibilidade e Ergonomia',
'Sinalização e Placas',
'Contratos e Licitações',
'QVT Laboral e Massagem',
'Erro1 Loja Virtual - Dependência Não Localizada',
'Materiais Expediente, Higiene e Limpeza'
];
// Check if current title is in exceptions
if (exceptionTitles.indexOf(current.title) == -1) {
return; // Not in exception list, exit
}
// Check if parent changed from empty to something (or if inserted with parent)
var hadNoParentBefore = !previous || !previous.parent;
var hasParentNow = current.parent && current.parent.toString() != '';
if (hadNoParentBefore && hasParentNow) {
// Find old categories with same title but no parent and active=true
var oldCat = new GlideRecord('sc_category');
oldCat.addQuery('title', current.title);
oldCat.addQuery('parent', '');
oldCat.addQuery('active', true); // assuming there is an active field
oldCat.query();
while (oldCat.next()) {
oldCat.active = false; // deactivate old orphan category
oldCat.update();
}
}
})(current, previous);
Reagrds,
Suyash