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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Can you try updating as this using setValue() method

current.setValue('u_empty', true);

OR

current.setValue('u_empty', false);

Regards
Ankur

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

I tried setting it to current.setValue('u_empty', true); but it does not change the field if i update the record.

The business rule runs after I insert or update a record.

Hi,

if that is after insert/update BR then you need to use current.update() but that is not recommended.

So please use before insert/update BR and check once.

Regards
Ankur

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

I set the business rule to run before a record is inserted or updated, but it is still not changing the value of u_empty.

Ok so now it changes the u_empty to true when it sees empty field, but when I fill the empty fields u_empty does not change back to false.