- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 07:22 AM
Hi,
I have created a new field in the Hr profile which will stores the employee work experience based on employee start date. This needs to be achieved through schedule job. I have also scripted the code to implement this. But it is not working in HR Profile table.
var cur = new GlideDateTime();
var gr = new GlideRecord('sn_hr_core_profile');
gr.addEncodedQuery();
gr.query();
while (gr.next()){
var start = gr.getValue('employment_start_date');
var dur = new GlideDuration(GlideDateTime.subtract(start, cur));
var inYears = dur.getDayPart() / 365;
var Years = parseInt(inYears);
gr.setValue('u_work_exp', Years);
gr.update();
}
Is there any issue on the code?
why it is not working in HR Profile table. Kindly help with the solution.
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 11:39 PM
Hi @Priya14 ,
Please try below code.
Please mark helpful & accept answer if it's really worthy for you.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 08:53 PM
@Priya14 This script should work for you.
var cur = new GlideDateTime();
var gr = new GlideRecord('sn_hr_core_profile');
//gr.addEncodedQuery(); add your query here
gr.query();
while (gr.next()) {
var start = new GlideDateTime(gr.getValue('employment_start_date'));
var dur = new GlideDuration(GlideDateTime.subtract(start, cur));
var inYears = dur.getDayPart() / 365;
var Years = parseInt(inYears);
gr.setValue('u_work_exp', Years);
gr.update();
}
In your script the issue was there at line number 9
var start = gr.getValue('employment_start_date');
This line returns a string value of date and in order to perform subtract on a date time you need to convert the string date to GlideDateTime. Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 11:39 PM
Hi @Priya14 ,
Please try below code.
Please mark helpful & accept answer if it's really worthy for you.