Where am I going wrong in this Bussines Rule code?

luizmarcond
Tera Contributor

I need that, every time I send an email that is related to the incident table, the incident record increment field is incremented by 1, so I can update the email subject automatically using an email template. But I can't write the code at all, it never works.

 

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

// Check if the email was sent and is related to an incident

var incident = new GlideRecord('incident');
// document_key contains the sys_id of the incident
if (incident.get(current.sys_id)) {
// Increment the email sent counter
if (incident.u_incremento != null) {
incident.u_incremento++;

 

 

 

 


} else {
incident.u_incremento = 1; // Initialize with 1 if it doesn't exist
}
incident.update(); // Update the incident record
}

})(current, previous);

 

 

Pics:

luizmarcond_0-1740945536127.png

luizmarcond_1-1740945583753.png

 

Thanks for trying help me! 😃

 

3 REPLIES 3

Itallo Brandão
Kilo Guru

Olá, Luiz!
Tudo bem? Espero que sim!

Percebi que é Brasileiro, vou te responder em português pra facilitar! 

Então vamos lá, a linha 7 do seu script está da seguinte forma:

 

if(incident.get(current.sys_id)){

 

 

Ao trabalhar com uma Business Rule na tabela Email (sys_email) , você normalmente precisa usar o campo document_key (ou  instance_id, dependendo da sua versão) para obter o registro do Incidente. O current.sys_idna sys_emailtabela na verdade se refere ao próprio sys_id do e-mail, não ao Incidente. Segue um script de exemplo que deve incrementar o u_incremento no Incidente correspondente sempre que um e-mail de saída para esse Incidente for inserido em sys_email:

 

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

    var inc = new GlideRecord('incident');
    if (inc.get(current.document_key)) {

        if (inc.u_incremento) {
            inc.u_incremento++;
        } else {
            inc.u_incremento = 1; 
        }
        inc.update(); 
    }
})(current, previous);

 


Resumindo:
Na linha 7 do seu script, troque o current.sys_id por current.document_key (ou current.instance_id, dependendo da sua versão).

Espero que ter te ajudado!
Se essa resposta for útil, marque como útil! 
TMJ!!!

Brad Bowman
Kilo Patron
Kilo Patron

'Current' refers to the record on the sys_email table in this case, that was just created, so with this line:

if (incident.get(current.sys_id)) {

you are attempting to retrieve a sys_email record when looking up records on the incident table.  It seems like what you should be using here is:

if (incident.get(current.instance)) {

since 'instance' is the column name of the document ID type field labeled 'Target' (English, out of box).

Ankur Bawiskar
Tera Patron
Tera Patron

@luizmarcond 

Having business rule on OOB sys_email table is not recommended.

Why not increment the field from the place where it's getting triggered i.e. business rule if you are using gs.eventQueue() etc?

Now for your script you are using wrong field to get the incident sysId

Refer what @Brad Bowman has mentioned and it will work for you.

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