How to Pull the Current User's Birthdate and Use It in a Client Script?

Danielle Freem1
Kilo Contributor

I'm trying to create an OnChange Catalog Client Script that pulls the current logged-in user's birthday, then subtracts the current date from it to get a specific age to use as a parameter that determines whether to keep a checkbox ticked. However, I am having a lot of trouble since I'm still a novice at coding.

What I need is:

1. The proper code components and syntax for a script include to call up the current user's date of birth from the users table.

2. The proper way to bring up the current date either through a script include or directly in the client script.

3. The proper way to format the final catalog client script to perform the necessary calculation for my code to work without it breaking my existing catalog UI policies. I have ones set to hide or show certain elements depending on what option in the catalog item the user has selected, and previous client scripting attempts have disrupted their functionality. I'd like to avoid that.

If you can share each of these steps one-by-one for me, with clear instructions on what parts reference what in terms of tables and fields, I would very much appreciate it.

1 ACCEPTED SOLUTION

Mahendra RC
Mega Sage

Hello Danielle,

If you want this calculation to happen on change of some field then use onChange Client script else use onLoad Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue) {
        var ga = new GlideAjax('CustomClientDateTimeUtils');
        ga.addParam('sysparm_name', 'validateUserBirthDay');
        ga.getXMLAnswer(parseRecievedResponse);
    } 

	function parseRecievedResponse(answer) {
		if (answer) {
			var ageLimit = 25; // replace age limit here
			if (parseInt(answer) > ageLimit) {
				//write your logic here
			}
		}
	}
}

Script Include where client callable is checked

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

	validateUserBirthDay: function() {
		var userAge = 0;
        var recordGR = new GlideRecord("sys_user");
		var todayDate = new GlideDateTime();
        if (recordGR.get(gs.getUserID())) {
			var birthDay = recordGR.getValue("birth_day"); //replace your variable
			var birthDayGDT = new GlideDateTime(birthDay);
			var dateDifference = todayDate.getNumericValue() - birthDayGDT.getNumericValue();
			userAge = dateDifference / (365*24*60*60*1000);
        }
		return userAge;
    },

    type: 'CustomClientDateTimeUtils'
});

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

What script did you start with?

1) you can use onLoad client script + GlideAJax

2) In the GlideAjax function get logged in user's date of birth; subtract as current date - date of birth and return true/false

3) based on that set the checkbox

regards
Ankur

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

Mahendra RC
Mega Sage

Hello Danielle,

If you want this calculation to happen on change of some field then use onChange Client script else use onLoad Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue) {
        var ga = new GlideAjax('CustomClientDateTimeUtils');
        ga.addParam('sysparm_name', 'validateUserBirthDay');
        ga.getXMLAnswer(parseRecievedResponse);
    } 

	function parseRecievedResponse(answer) {
		if (answer) {
			var ageLimit = 25; // replace age limit here
			if (parseInt(answer) > ageLimit) {
				//write your logic here
			}
		}
	}
}

Script Include where client callable is checked

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

	validateUserBirthDay: function() {
		var userAge = 0;
        var recordGR = new GlideRecord("sys_user");
		var todayDate = new GlideDateTime();
        if (recordGR.get(gs.getUserID())) {
			var birthDay = recordGR.getValue("birth_day"); //replace your variable
			var birthDayGDT = new GlideDateTime(birthDay);
			var dateDifference = todayDate.getNumericValue() - birthDayGDT.getNumericValue();
			userAge = dateDifference / (365*24*60*60*1000);
        }
		return userAge;
    },

    type: 'CustomClientDateTimeUtils'
});

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

I used your examples as my base. However, the code is still not executing. I've made sure that the script include is Client Callable and my Client Script is set to Active with Isolate Script turned off. Here is what my code looks like:

Script Include:

var BirthdayGrabber = Class.create();
BirthdayGrabber.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	
	grabBirthday: function(){
		var userAge = 0;
        var recordGR = new GlideRecord("sys_user");
		var todayDate = new GlideDateTime();
        if (recordGR.get(gs.getUserID())) {
			var birthDay = recordGR.getValue("u_date_of_birth"); //replace your variable
			var birthDayGDT = new GlideDateTime(birthDay);
			var dateDifference = todayDate.getNumericValue() - birthDayGDT.getNumericValue();
			userAge = dateDifference / (365*24*60*60*1000);
        }
		return userAge;
	},

    type: 'BirthdayGrabber'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    } 
	if (newValue == 'true') {
        var ga = new GlideAjax('BirthdayGrabber');
        ga.addParam('sysparm_name', 'validateUserBirthDay');
        ga.getXMLAnswer(parseRecievedResponse);
    } 

	function parseRecievedResponse(answer) {
		if (answer) {
			var ageLimit = 18; // replace age limit here
			if (parseInt(answer) < ageLimit) {
				alert("Nice Try! Please pick another game.");
			}
		}
	}
}

 

The Alert is a test to make sure the code is working. Should I replace 'validateUserBirthDay' with a different parameter? Is 'u_date_of_birth' (the system name for the field I'm trying to check)  the correct input? Does the fact that this is being applied to a scoped application have something to do with the code failure?

Hello Danielle,

Yes you need to replace the validateUserBirthDay with grabBirthday

Thanks