Business rule issue

maheshchokkara
Tera Contributor

I have a business rule when a parent state is updated it will updated the related child record states now the issue is when the parent have no child records I'm showing an error "No child records present" otherwise I'm updating the child records. here issue is even after having the child records under a parent still it is showing error message and updating the state both things are running what mistake I made help me
when to run  "AFTER UPDATE"

 

(function executeRule(current, previous /*null when async*/ ) {
 
    if (previous.state != current.state) {
        var fo = new GlideRecord('x_1086067_financia_financial_object');
        fo.addQuery('parent', current.sys_id);
        fo.query();
        if (fo.hasNext()) {
            while (fo.next()) {
                fo.state = current.state;
                fo.update();
            }

        }else {
        gs.addErrorMessage('No child recods present');
        }
    }
   
})(current, previous);
9 REPLIES 9

Anil Nemmikanti
Giga Guru

Hi @maheshchokkara , 

Please make the BR as before BR update, it will work.

 

Thanks for the response I Tried No luck.

current.state.changes() please use the fallowing method in the if condition.

Ankur Bawiskar
Tera Patron
Tera Patron

@maheshchokkara 

update as this

(function executeRule(current, previous /*null when async*/ ) {
    if (previous.state != current.state) {
        var childRecords = new GlideRecord('x_1086067_financia_financial_object');
        childRecords.addQuery('parent', current.sys_id);
        childRecords.query();
        var hasChildren = false;
        while (childRecords.next()) {
            hasChildren = true;
            childRecords.state = current.state;
            childRecords.update();
        }
        
        if (!hasChildren) {
            gs.addErrorMessage('No child records present');
        }
    }
})(current, previous);

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