Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Example for business rule script to fire event sys_journal_field for specific table

EvgeniyG
Tera Contributor

Hey,
I have a busineness rule to fire event on insert to 

sys_journal_field that looks like this:

 

(function executeRule(current, previous /*null when async*/) { 
if (current.getTableName() == 'sys_journal_field' && current.operation() === 'insert')
    gs.eventQueue("cato.sys_journal_field.inserted", current, gs.getUserID(), gs.getUserName());
if (current.getTableName() == 'sys_journal_field' && current.operation() === 'update')
    gs.eventQueue("cato.sys_journal_field.updated", current, gs.getUserID(), gs.getUserName());
})(current, previous);

 

 

is it possible to add a condition, that event will be fired only if "name" field of sys_journal_field would be "incident" for example?

Thank you,
Evgeniy
1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@EvgeniyG 

Yes, you can add a condition to your business rule to check if the "name" field of the sys_journal_field record is equal to "incident" before firing the event:

 

(function executeRule(current, previous /*null when async*/) { 
    if (current.getTableName() == 'sys_journal_field') {
        if (current.operation() === 'insert' && current.name == 'incident') {
            gs.eventQueue("cato.sys_journal_field.inserted", current, gs.getUserID(), gs.getUserName());
        }
        if (current.operation() === 'update' && current.name == 'incident') {
            gs.eventQueue("cato.sys_journal_field.updated", current, gs.getUserID(), gs.getUserName());
        }
    }
})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

3 REPLIES 3

Harish KM
Kilo Patron
Kilo Patron

Hi @EvgeniyG you can do like below name field on journal table holds table name

 

if (current.getTableName() == 'sys_journal_field' && current.name == 'incident' && current.operation() === 'insert')
gs.eventQueue("cato.sys_journal_field.inserted", current, gs.getUserID(), gs.getUserName());

Regards
Harish

Maddysunil
Kilo Sage

@EvgeniyG 

Yes, you can add a condition to your business rule to check if the "name" field of the sys_journal_field record is equal to "incident" before firing the event:

 

(function executeRule(current, previous /*null when async*/) { 
    if (current.getTableName() == 'sys_journal_field') {
        if (current.operation() === 'insert' && current.name == 'incident') {
            gs.eventQueue("cato.sys_journal_field.inserted", current, gs.getUserID(), gs.getUserName());
        }
        if (current.operation() === 'update' && current.name == 'incident') {
            gs.eventQueue("cato.sys_journal_field.updated", current, gs.getUserID(), gs.getUserName());
        }
    }
})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Dipen Wadhwana
Giga Guru

Hi @EvgeniyG ,

 

In this optimized version, the conditions for the table name, operation type, and the "name" field value are combined into a single if statement. Additionally, the event name is dynamically generated based on the operation type (insert or update). This results in a more streamlined and efficient code while still achieving the desired functionality.

 

(function executeRule(current, previous /*null when async*/) { 
    if (current.getTableName() == 'sys_journal_field' && (current.operation() === 'insert' || current.operation() === 'update') && current.name == 'incident') {
        gs.eventQueue("cato.sys_journal_field." + current.operation(), current, gs.getUserID(), gs.getUserName());
    }
})(current, previous);

 

Please mark this response as helpful if your question has been answered correctly.