Sandeep Rajput
Tera Patron

@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.