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

Hello @Danielle 

Just wanted to check with you, if the above response answered your question. If you feel your question is answered, then please do close this thread/question by marking the appropriate response as correct.

Thanks