The CreatorCon Call for Content is officially open! Get started here.

Business rule current.field not working

Bob Bobberson
Kilo Contributor

For a business rule why does current.field = "any string" not actually update the field to that string even if a condition is met.

Here is my script, I have debugged it and it reaches current.u_empty = "false"; but it does not actually set the value to "false" it just leaves it at undefined.

(function executeRule(current, previous /*null when async*/) {
	
	var gr = new GlideRecord("cmdb_ci_business_app");
	
	gr.query();
	
	while(gr.next()) {
		
		var fields = gr.getFields();
		
		for(var i = 0; i < fields.size(); i++) {
			
			var ge = fields.get(i);
			
			if(ge.nil()) {

				current.u_empty = "true";
				break;

			} else {

				current.u_empty = "false";
			
			}
		}
	}
})(current, previous);
1 ACCEPTED SOLUTION

Hi,

for some field it must be going to if and hence keeping it true

can you try this and check once

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

	var flag = false;
	var field;

	var gr = new GlideRecord("cmdb_ci_business_app");
	//no need for any specific filters here.
	gr.query();
	while(gr.next()) {

		var fields = gr.getFields();
		for(var i = 0; i < fields.size() - 1; i++) {
			var ge = fields.get(i);
			var descriptor = gr.getED();
			var fieldName = descriptor.getName();

			var fieldValue = gr[fieldName].getDisplayValue();
			if(!fieldName.startsWith('sys')){
				if(fieldValue.nil()) {
					flag = true;
					field = fieldName;
					break;

				} else {
					flag = false;
				}
			}
		}
	}

	if(flag){
		gs.info('Field ' + field);
		current.setValue('u_new', true);
	}
	else{
		current.setValue('u_new', false);
	}

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

22 REPLIES 22

I changed var descriptor = gr.getED() to var descriptor = ge.getED() and it works it skips all the sys_ fields, but current.setValue("u_new", false); is not changing the value to false. I tried debugging again to see what the issue was but all the fields it iterated through were filled and there was nothing undefined.

Hi,

If it was already false then it won't have any impact

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

The record was first true because there were empty fields but then when I fill out the fields it does not change back to false.

Hi,

if you fill out the fields with values it won't go inside the if since it is checking for nil

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

    var gr = new GlideRecord("cmdb_ci_business_app");
    //no need for any specific filters here.
    gr.query();
    while(gr.next()) {

        var fields = gr.getFields();
        for(var i = 0; i < fields.size() - 1; i++) {
            var ge = fields.get(i);
            var descriptor = gr.getED();
            var fieldName = descriptor.getName();

            var fieldValue = gr[fieldName].getDisplayValue();
            if(!fieldName.startsWith('sys')){
                if(fieldValue.nil()) {
                    current.setValue('u_new', true);
                    break;
                } else {
                    current.setValue('u_new', false);
                }
            }
        }
    }

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

wouldnt it go to the else statement if the fields are not null? Once I fill out all the fields it goes to the else statement but it does not set the value to false it keeps it at true.