Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Scheduled job is not working for HR Profile

Priya14
Tera Contributor

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

 

 
 
1 ACCEPTED SOLUTION

abirakundu23
Giga Sage

Hi @Priya14 ,

Please try below code.

abirakundu23_0-1714113496792.png

Please mark helpful & accept answer if it's really worthy for you.

View solution in original post

6 REPLIES 6

Maddysunil
Kilo Sage

@Priya14 

  • 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

 

I have tried this code and also added the gs.info() to check the logs 

it is showing an error 

 

Evaluator.evaluateString() problem: com.glide.script.fencing.MethodNotAllowedException: Function log is not allowed in scope sn_hr_sp. Use gs.debug() or gs.info() instead: com.glide.script.fencing.ScopedScriptableObject.checkWhitelistAccess(ScopedScriptableObject.java:137)
com.glide.script.fencing.ScopedScriptableObject.shouldAllow(ScopedScriptableObject.java:105)
com.glide.script.fencing.WrappedScriptableObject.get(WrappedScriptableObject.java:57)
org.mozilla.javascript.ScriptableObject.getProperty(ScriptableObject.java:2097)
org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2394)
org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2380)
org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1349)
org.mozilla.javascript.Interpreter.interpret(Interpreter.java:830)
org.mozilla.javascript.InterpretedFunction.lambda$call$0(InterpretedFunction.java:160)
com.glide.caller.gen._refname_.call(Unknown Source)
com.glide.script.ScriptCaller.call(ScriptCaller.java:22)
org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:159)
org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:597)
org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3573)
org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:172)
com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:389)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:199)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:131)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:122)
com.glide.script.Evaluator.evaluateString(Evaluator.java:103)
com.snc.automation.ScriptJob.executeInSingleDomain(ScriptJob.java:77)
com.snc.automation.ScriptJob.execute(ScriptJob.java:47)
com.glide.schedule.JobExecutor.lambda$executeJob$0(JobExecutor.java:169)
com.glide.schedule.JobExecutor.executeJob(JobExecutor.java:172)
com.glide.schedule.JobExecutor.execute(JobExecutor.java:155)
com.glide.schedule.JobExecutor.execute(JobExecutor.java:149)
com.glide.schedule_v2.SchedulerWorkerThread.executeJob(SchedulerWorkerThread.java:449)
com.glide.schedule_v2.SchedulerWorkerThread.lambda$process$1(SchedulerWorkerThread.java:318)
com.glide.worker.TransactionalWorkerThread.executeInTransaction(TransactionalWorkerThread.java:35)
com.glide.schedule_v2.SchedulerWorkerThread.process(SchedulerWorkerThread.java:318)
com.glide.schedule_v2.SchedulerWorkerThread.run(SchedulerWorkerThread.java:118)

 

 

Thanks

@Priya14 

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

 

 

This is the Updated Code
 
// Get the current date and time
var cur = new GlideDateTime();
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();
    gs.info(yearsOfExperience);
}
 
Thanks