Business Rule

PolyannaS
Tera Contributor

I am having a bit of difficulty drafting a group assignment blocking rule within a BR.

The rule should work as follows:

Only N2 groups should have permission to assign tickets to N3. IF an N3 group tries to assign tickets to other N3 groups, they should be blocked and shown a message directing them to N2. IF any other group tries to assign a ticket to an N3 group, the block, message, and redirection should be applied as well. The rule should only allow N2 groups to assign tickets to N3.

See the script below:
BR = 
Before - Update/Insert



 
(function executeRule(current, previous /*null when async*/ ) {

    // --- ARRAYS DE GRUPOS (use sys_id) ---
    var vGroupsN2 = [
     'sys_id_group'
  ];

    var vGroupsN3 = [
 'sys_id_group',
    ];

    // Só valida quando o assignment_group mudar
    if (!current.assignment_group.changes()) {
        alert('Teste 1');
        return;
    }

    // Se o novo grupo NÃO está entre os N3, não há regra a aplicar
    var newGroupId = String(current.assignment_group);
    if (vGroupsN3.indexOf(newGroupId) === -1) {
        alert('Teste 2');
        return;
    }

    // Verifica de onde está saindo: precisa ser de um N2
    var prevGroupId = (previous && !previous.assignment_group.nil()) ? String(previous.assignment_group) : '';
    var isPrevFromN2 = vGroupsN2.indexOf(prevGroupId) > -1;

    // Se não veio de N2, bloqueia a atribuição para N3
    if (!isPrevFromN2) {
        var newGroupName = getGroupNameSafe(newGroupId);
        var prevGroupName = prevGroupId ? getGroupNameSafe(prevGroupId) : '—';
        alert('teste 3');
        var msg = 'MENSAGEM';

        gs.addErrorMessage(msg);

        // Reverte valor (em update) e aborta transação
        if (!current.isNewRecord()) {
            current.assignment_group = previous.assignment_group;
        }
        current.setAbortAction(true);
        alert('Teste 4');
        return;
    }

    // --- Função utilitária para obter o nome do grupo com segurança ---
    function getGroupNameSafe(sysId) {
        try {
            var gr = new GlideRecord('sys_user_group');
            if (gr.get(sysId)) {
                return gr.getDisplayValue('name') || sysId;
            }
        } catch (e) {
            // ignora erros e retorna o sys_id
        }
        return sysId;
    }

})(current, previous);
1 REPLY 1

Tanushree Maiti
Kilo Sage

Redirection requirement needs more clarity. redirects to which group?

Sample code/configuration could be 

 

Navigate to System Definition > Business Rules and create a new rule.
Table: Incident [incident]
Advanced: Checked
When: Before (Insert and Update)
Condition: Assignment group | Changes to | [Sys_id of N3]
Script:

 

(function executeRule(current, previous /*null when async*/) {
// Check if user is NOT in N2
if (!gs.getUser().isMemberOf('<sys_id of N2>')) {
gs.addErrorMessage('Only members of N2 can assign to N3.');
current.setAbortAction(true);
}
})(current, previous);

Please mark this response as Helpful & accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: