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

Allen Andreas
Administrator
Administrator

Hi,

is this in a "before" business rule?

If checkbox type field, you should be fine using:

current.u_empty = true;

Notice the removal of double quotes.

Please mark reply as Helpful/Correct, if applicable. Thanks!


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

The business rule runs after a record has been updated, inserted, or deleted. I also tried removing the quotations and just made it current.u_empty = true; but it still does not change the value.

Hi,

You mean the business rule has it's setting as an "After" business rule. Not "Before", correctly? I just wanted to make sure we're talking about the same thing because a business rule runs after...an update or insert, but there's a different "After" or "Before" I'm talking about.

You'd want to consider changing this business rule as I mentioned above to a "before".

You could technically still do it in an "after" business rule, but you'd want to follow documentation here: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0693812

Please mark reply as Helpful/Correct, if applicable. Thanks!


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

Hi,

You have another post on the forums, sort of in-line with this, in which you're asking how to remove or ignore the "Tags" field (aka sys_tags): https://community.servicenow.com/community?id=community_question&sys_id=00fd41fcdbfc341014d6fb243996...

I've given you guidance there on how to remove this from your array of fields, so that you don't iterate through it in your for loop...

This line here...sets your "fields" JavaScript variable to an array of fields via getFields()

var fields = gr.getFields();

Then, you'd want to splice out the tag element within the array by using something like:

fields.splice(fields.indexOf('sys_tags'), 1);

Then, within your for loop you shouldn't have any issue there, but ultimately, if you're getting an error with something like: TypeError is undefined, then you'd want to remove them from your array or give them a value so you don't get this error.

Please mark reply as Helpful/Correct, if applicable. Thanks!


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