Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to check if decimal field is null or not

Nishant Kumar5
Tera Contributor

We have a decimal field which can either contain values from 0-10 or it can be empty.

let impactRiskScoreExists = !gr.u_impact_risk_score.nil();
if(impactRiskScoreExists){
    // Further process
}

The issue is that sometimes in case the value is 0, the value of impactRiskScoreExists is either true or false. Is there any other way to validate for a decimal type field value?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Nishant Kumar5 

try this

var impactRiskScore = gr.u_impact_risk_score;
var impactRiskScoreExists = impactRiskScore !== null && impactRiskScore !== undefined;

if (impactRiskScoreExists) {
    // Further process
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Nishant Kumar5 

try this

var impactRiskScore = gr.u_impact_risk_score;
var impactRiskScoreExists = impactRiskScore !== null && impactRiskScore !== undefined;

if (impactRiskScoreExists) {
    // Further process
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Rajesh Chopade1
Mega Sage

hi @Nishant Kumar5 

If your concern is that the field can be within a specific range (0-10), you could also ensure the value falls within that range by using a condition like this:

let impactRiskScore = gr.u_impact_risk_score;
let impactRiskScoreExists = impactRiskScore !== null && impactRiskScore !== undefined && impactRiskScore >= 0 && impactRiskScore <= 10;
if (impactRiskScoreExists) {
    // Further process
}

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh

Nishant8
Giga Sage
Giga Sage

Hello @Nishant Kumar5, If you'd like to just validate whether this field is null or not, then you can try below:

var impactRiskScoreValue = gr.getValue('u_impact_risk_score');
var impactRiskScoreExists = impactRiskScoreValue != null && impactRiskScoreValue != undefined;

Rafael Piao
Tera Contributor

super easy, just use gs.nil method.

It will return true only the field is empty.

var gr = new GlideRecord('incident');
if (gr.get('57af7aec73d423002728660c4cf6a71c')) {
	if (gs.nil(gr.u_score)) {
                // only run no value on this field
		gs.print('score is null');
	} else {
		gs.print("score: " + gr.u_score);
	}
}