- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2022 08:21 AM
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:
Could anyone help me with this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2022 09:04 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2022 08:46 AM - edited 10-10-2022 08:48 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2022 08:54 AM
In the Script Include, I don't think dob.getDisplayValue() actually getting the value you want. Does dropping the .getDisplayValue() fix that for you?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2022 09:04 AM
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