Business rule issue

Not applicable

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

Not applicable

No luck, Still the same.

maheshchokkara_0-1749629189711.png

 

Anil Nemmikanti
Tera Guru

It is a After Update Br means it will get executed after the record updated in the system. At that time previous object will not be available so that's the reason. 

Anil Nemmikanti
Tera Guru

It is a After Update Br means it will get executed after the record updated in the system. At that time previous object will not be available so that's the reason. 

Ehab Pilloor
Mega Sage

Hi @Community Alums,

 

Add Condition to your BR that it should run when State changes. And run the following

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

That way BR will trigger if State is changed and record is inserted.

 

Regards,

Ehab Pilloor

Anil Nemmikanti
Tera Guru
(function executeRule(current, previous /*null when async*/ ) {
	//gs.addErrorMessage("hello");
    if (current.state.changes()) {
        var childRecords = new GlideRecord('incident');
        childRecords.addQuery('parent_incident', current.sys_id);
        childRecords.query();
		var hasChildren;
		if(childRecords.hasNext()){
        while (childRecords.next()) {
            childRecords.state = current.state;
			childRecords.setWorkflow(false);
            childRecords.update();

        }
        
		}
    else{
	//gs.addInfoMessage(hasChildren);
            gs.addErrorMessage('No child records present');
        }
	}
})(current, previous);

Hi  @Community Alums ,    

 

Use the above code I tested in my PDI , please change the table name and the parent field name