Calculate user's age

Vinicius2
Kilo Sage

Hello everyone,

I have two date fields on sys_user (u_birthday, u_age)

Subtracting these values will tell me the age of the user at the time the request was received. 

I have created the following client script and script include but it is not working properly.

Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var gaAge = new GlideAjax('academyUtils');
    gaAge.addParam('sysparm_name', 'checkAge');
    gaAge.addParam('sysparam_id', newValue);
    gaAge.getXML(updateAge);
}

function updateAge(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    alert(answer);
    var a = parseInt(answer);
    alert(a);
    g_form.setValue("u_age", a.toPrecision(2));

}

 Script Include

var academyUtils = Class.create();
academyUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkAge: function() {
        var dob = this.getParameter('sysparam_id');
        var gdt1 = new GlideDateTime();
        var gdt2 = new GlideDateTime(dob.getDisplayValue());
	
        duration = GlideDateTime.subtract(gdt2, gdt1).getDayPart();
        duration = parseInt(duration / 365); // Years
		
        return duration;
    },


    type: 'academyUtils'
});

Result:

Vinicius2_0-1665415244074.png

Could anyone help me with this?

1 ACCEPTED SOLUTION

Sagar Pagar
Tera Patron

Hi,

 

Try this updated script include scripts -

 

var academyUtils = Class.create();
academyUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkAge: function() {
        var dob = this.getParameter('sysparam_id');
        var gdt1 = new GlideDateTime();
        var gdt2 = new GlideDateTime(dob);
	
        duration = GlideDateTime.subtract(gdt2, gdt1).getDayPart();
        duration = parseInt(duration / 365); // Years
		
        return duration;
    },


    type: 'academyUtils'
});

 

Thanks,
Sagar Pagar

The world works with ServiceNow

View solution in original post

3 REPLIES 3

Avinash_Patil1
Giga Guru
Try Below script in Script include
 
var dob =this.getParameter('sysparam_id');
var today = new GlideDateTime();
var todayYear = today.getYearLocalTime();
var bday = new GlideDateTime(dob.toString());
var bdayYear = bday.getYearLocalTime();
var age =todayYear - bdayYear;
gs.info(age);

Claude DAmico
Kilo Sage

In the Script Include, I don't think dob.getDisplayValue() actually getting the value you want. Does dropping the .getDisplayValue() fix that for you?

Claude E. D'Amico, III - CSA

Sagar Pagar
Tera Patron

Hi,

 

Try this updated script include scripts -

 

var academyUtils = Class.create();
academyUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkAge: function() {
        var dob = this.getParameter('sysparam_id');
        var gdt1 = new GlideDateTime();
        var gdt2 = new GlideDateTime(dob);
	
        duration = GlideDateTime.subtract(gdt2, gdt1).getDayPart();
        duration = parseInt(duration / 365); // Years
		
        return duration;
    },


    type: 'academyUtils'
});

 

Thanks,
Sagar Pagar

The world works with ServiceNow