Script to hide category from field

Ariomar de Deus
Tera Contributor

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

 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Ariomar de Deus 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

SuyashJ41731830
Tera Contributor

Hi
@Ariomar de Deus 

(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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Ariomar de Deus 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

SuyashJ41731830
Tera Contributor

Hi
@Ariomar de Deus 

(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