- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2016 07:28 AM
All,
I have been on the forum getting examples to populate a 'string' field called age to display the age of the person filling out the catalog item. I have a Date variable and when they type in or select from calendar the 'age' string field should populate with their age.
I have created a script include and a client script that calls it, but my issue is that how do I get rid of the decimal in my string field? Is it due to only using a date variable instead of a date/time variable. I have copied my scripts below:
Any suggestions would help me out.
Script Include:
var AjaxGetAge = Class.create();
AjaxGetAge.prototype = Object.extendsObject(AbstractAjaxProcessor, {
birthCalc: function() {
var db = new GlideDateTime(this.getParameter('sysparm_strt'));
var datebirth = gs.dateDiff(db,gs.nowDate(), true);
gs.log('Difference: '+datebirth);
var seconds = parseInt(datebirth, 10);
var whole = seconds/(365*24*3600);
var convert = Math.floor(whole);
gs.log('Years: '+convert);
return convert;
}
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
var birth = g_form.getValue('date_of_birth');//set this as purchase date
var ajax = new GlideAjax('AjaxGetAge');
ajax.addParam('sysparm_name','birthCalc');
ajax.addParam('sysparm_strt',birth);
//ajax.addParam('sysparm_end',end);
ajax.getXMLWait();
var answer = ajax.getAnswer();
g_form.setValue('age', answer);
}
Screenshot of output on catalog item
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2016 07:34 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2016 10:30 AM
youssef,
This was helpful as well. Since this will be geared towards students. We are ok right now with just using toPrecision() function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2017 04:35 AM
Hi Brett,
I have followed the the same steps as mentioned above. Age doesn't populate automatically.
Could you help, please?
Thanks in advance,
Kaushik.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2017 07:25 AM
Figured it out.
Thank you