Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Check if field is a specific value or empty

Mark Vancil2
Mega Guru

Using this is an IF check on a workflow, and not sure where I'm going wrong. It's not catching when the field is empty.

 

var gr = new GlideRecord("sys_user");
gr.get(current.variables.request_for.toString());
gs.warn(gr.u_jobfamily);

if (gr.u_jobfamily == 'SB') {
   answer = 'yes';
	
} 
else if (gr.u_jobfamily.nil()) {
	answer = 'yes';
}
else {
	answer = 'no';

}
1 ACCEPTED SOLUTION

Thanks for the update. However if you feel the issue is still there you can give a try with below script:

answer = ifScript();
function ifScript(){
	var gr = new GlideRecord("sys_user");
	gr.get(current.variables.request_for.toString());
	gs.warn(gr.u_jobfamily);
	
	if (gr.u_jobfamily == 'SB') {
		return 'yes';
		
	}
	else if (gr.u_jobfamily.nil()) {
		return 'yes';
	}
	else {
		return 'no';
		
	}
}

 

Regards,

Alok

Kindly mark my answer as Correct and helpful based on the Impact.
Thanks and Regards,
Alok

View solution in original post

11 REPLIES 11

Tony Chatfield1
Kilo Patron

Hi, have you tried testing against an empty string

if(gr.u_jobfamily == '')

 

Yeah, I probably should have mentioned, it was that way when I noticed that an empty value got through, and I changed it to the code above while troubleshooting it.

In both versions, empty values gets through.

Alok Ranjan Das
Tera Guru

Hi Mark,

What type of field is "u_jobfamily"? Is the rest functionality working as expected? Could you please give a try using the below code:

gr.u_jobfamily=='' // instead of gr.u_jobfamily.nil()

Kindly mark my answer as Correct and Helpful based on the Impact.

Regards,

Alok

Kindly mark my answer as Correct and helpful based on the Impact.
Thanks and Regards,
Alok

Yeah, I probably should have mentioned, it was that way when I noticed that an empty value got through, and I changed it to the code above while troubleshooting it.

In both versions, empty values gets through.