My registration is not updating

jkelvynsant
Tera Contributor

Hello everyone! I developed this br and the fields within the glide are updating, but the update of the "state" and "u_stage" fields of the current record are not updating. Can anyone tell me what the problem could be?

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

    try {
        var taskGR = new GlideRecord('x_acel2_csc_csc_task');
        taskGR.addQuery('parent', current.sys_id);
        taskGR.addQuery('state', '5');
        taskGR.addQuery('short_description', 'Tarefa de cadastro de contraparte');
        taskGR.query();


        while (taskGR.next()) {

            taskGR.work_notes = current.comments.getJournalEntry(1);
            taskGR.state = '2';
            taskGR.update();

            gs.message('Tarefa ' + taskGR.number + ' atualizada com resposta do solicitante',
                'CIM0003428');
        }

        current.state = '2'; //THIS LINE
        current.u_stage = '2'; //THIS LINE


        gs.message('Solicitação ' + current.number + ' retornou para execução após resposta',
            'CIM0003428');

    } catch (e) {
        gs.message('Erro na Business Rule Resposta Solicitante: ' + e.message, 'CIM0003428');
    }


})(current, previous);
3 REPLIES 3

GlideFather
Tera Patron

Hi @jkelvynsant 

 

current.state = '2'; //THIS LINE
current.u_stage = '2'; //THIS LINE

 

Here you are assigning the value "2" to the State and Stage fields, however after the operation you need to update the record to reflect the change.

 

BUT (!!!) you shouldn't use current.update() in a business rule

 

but you can try adding this:

current.state = '2';
current.u_stage = '2';
current.setWorkflow(false);
current.update();
current.setWorkflow(true);

 

setWorkflow(false); will stop the other business rule from execution and setWorkflow(true); will enable it again.

It is to avoid recursions where the business rules would might be triggering one another:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0715782  

 

Let me know if this helped you

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


SD_Chandan
Kilo Sage

Hi @jkelvynsant ,

 

As Kamil mention you can try this it will work 

current.setWorkflow(false);
current.update();
current.setWorkflow(true);


You can refer community article as well :-  
https://www.servicenow.com/community/developer-forum/current-update-menthod-in-business-rules/td-p/2...


Thank you
Chandan

Nishant8
Giga Sage

Hello @jkelvynsant, could you please verify whether BR is configured to run Before or After update? If its After update, then try to move following lines to Before update BR; you can keep update table, x_acel2_csc_csc_task, in after update.

        current.state = '2'; //THIS LINE
        current.u_stage = '2'; //THIS LINE

 

Regards,

Nishant