- 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 07:30 AM
- In your script, you have gr.addEncodedQuery();, which adds an empty query condition. This means your query will return all records from the sn_hr_core_profile table. If this is intentional and you want to process all records, it's fine. Otherwise, you need to specify a condition inside addEncodedQuery() to filter records based on specific criteria.
- Ensure that the employment_start_date field is being correctly retrieved from the HR Profile records (sn_hr_core_profile). Check if this field exists and contains valid dates for the employees.
- Verify that the u_work_exp field in the HR Profile table (sn_hr_core_profile) is of the correct data type to store the calculated work experience value. It should be a numeric field to store years of experience.
Also apply few logs to debug
// Get the current date and time
var cur = new GlideDateTime();
// Query HR Profile records
var gr = new GlideRecord('sn_hr_core_profile');
gr.query();
while (gr.next()) {
// Get the employment start date from the HR Profile record
var start = gr.getValue('employment_start_date');
// Calculate the duration between the start date and current date
var dur = GlideDateTime.subtract(start, cur);
// Calculate the number of years of work experience
var inYears = dur.getNumericValue() / (1000 * 60 * 60 * 24 * 365);
var yearsOfExperience = parseInt(inYears);
// Update the 'u_work_exp' field with the calculated value
gr.u_work_exp = yearsOfExperience;
// Update the HR Profile record
gr.update();
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 07:57 AM
I have tried this code and also added the gs.info() to check the logs
it is showing an error
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 08:02 PM
As error suggest please use gs.info() instead of gs.log(). Also share your updated code here.
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 10:47 PM - edited 04-25-2024 10:49 PM