Why Before Update Business rule working while insertion

Saranya2
Tera Contributor

Hi All,

->We have a before update business rule on change request table where we are checking when we don't have incident number added on any of these two related lists "Incident Fixed by Change" or "Incident Caused by Change", it is throwing error.

->But as per the business rule it should trigger only when updating the record, but it is also triggered while insertion.

-> We have checked we haven't used current.update() in any of the before insert/update business rule.

Not sure how it is happening, how to fix this issue? can anyone please suggest?

BR:

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

var cin = new GlideRecord("incident");
        cin.addEncodedQuery("rfc=" + current.sys_id + "^ORcaused_by=" + current.sys_id);
        cin.query();
        if(!cin.hasnext()) {
            gs.addErrorMessage('Emergency changes must have an associated Incident.  Please verify you have an associated Incident listed in either \"Incidents Caused by Change\", \"Incidents Fixed by Change\"');
            current.setAbortAction(true);
        } 
})(current, previous);

 

Thanks,

Saranya

 

1 ACCEPTED SOLUTION

Hi,

You may have other processes that are updating the record, thus, causing this to run (so the record is actually being updated by a later running business rule). You can consider making this business rule run as late as you can so it's last by adjusting the order. Refer to documentation for assistance: https://docs.servicenow.com/en-US/bundle/utah-application-development/page/script/general-scripting/... - you'd need to review your instance for what could be causing this to run and look at your other business rules, etc. Check for any current.update() in your after business rules as well (which shouldn't be there).


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

6 REPLIES 6

VaishnaviShinde
Kilo Sage

@Saranya2 

 

Try the below code

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

var cin = new GlideRecord("incident");
        cin.addEncodedQuery("rfc=" + current.sys_id + "^ORcaused_by=" + current.sys_id);
        cin.query();
        if(!cin.hasnext()) {
            gs.addErrorMessage('Emergency changes must have an associated Incident.  Please verify you have an associated Incident listed in either \"Incidents Caused by Change\", \"Incidents Fixed by Change\"');

            current.setWorkflow(false);
            current.setAbortAction(true);
        } 
})(current, previous);

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

Hi @VaishnaviShinde,

I have added current.setWorkflow(false); still it is triggering while insertion.

Hi @Saranya2 

Please add below code:

var cin = new GlideRecord("incident");
cin.addEncodedQuery("rfc=" + current.sys_id + "^ORcaused_by=" + current.sys_id);
cin.query();
gs.info('count of request:'+cin.getRowCount()+' '+current.number);
if (cin.next()) {
gs.info('In if');
}else{
gs.info('In else');
gs.addErrorMessage('Emergency changes must have an associated Incident. Please verify you have an associated Incident listed in either \"Incidents Caused by Change\", \"Incidents Fixed by Change\"');
current.setWorkflow(false);
current.setAbortAction(true);
}

 

For me it works. Please check let me know if you stuck

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

Allen Andreas
Administrator
Administrator

Hi,

If you're meaning that the business rule is running on insert and update and it shouldn't do that, then you'd want to uncheck the "Insert" box on this specific business rule configuration.

AllenAndreas_0-1679417298625.png

 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!