addDays function not working

dvelloriy
Kilo Sage
I am creating a new sys_trigger record and setting the next action based on effective date variable.
Effective data value is Apr 3, 2024. however when the flow is executed and sys_trigger record is created it shows current time stamp. Please advise.. I was expecting Mar 3, 2024.
 
var scheduledJob = new GlideRecord('sys_trigger');
    scheduledJob.initialize();
    scheduledJob.name = 'assign to Group B';
    scheduledJob.script = 'var gr = new GlideRecord("' + tableName + '"); if (gr.get("' + triggerRecordSysId + '")) { gr.assignment_group = "' + groupB + '";gr.state=10; gr.update(); }';
    scheduledJob.next_action = effectiveDate.addDaysLocalTime(-30);
    scheduledJob.insert();
 
dvelloriy_0-1740419644160.png

 

dvelloriy_1-1740419723579.png

 

 

 

8 REPLIES 8

priyatam_pvp
Tera Guru

Try the below code

var scheduledJob = new GlideRecord('sys_trigger');
scheduledJob.initialize();
scheduledJob.name = 'assign to Group B';

// Correcting the date calculation
var scheduledDate = new GlideDateTime(effectiveDate);
scheduledDate.addDaysLocalTime(-30);
scheduledJob.next_action = scheduledDate.getValue(); // Ensure correct assignment

// Assign the script to update the record
scheduledJob.script = 'var gr = new GlideRecord("' + tableName + '"); if (gr.get("' + triggerRecordSysId + '")) { gr.assignment_group = "' + groupB + '";gr.state=10; gr.update(); }';

scheduledJob.insert();

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Priyatam Patchipulusu


Thanks @priyatam_pvp Its working now, however the script is not getting executed. I manually changed the next action to 5 mins from now. waited for it to run, however the state is red "ready". It did not execute the script.

 

'var gr = new GlideRecord("' + tableName + '"); if (gr.get("' + triggerRecordSysId + '")) { gr.assignment_group = "' + groupB + '";gr.state=10; gr.update(); }';

DrewW
Mega Sage
Mega Sage

So how did you define the var "effectiveDate"?  I do not see that in your code.

Hi Drew, this is defined in a flow:

 

var triggerRecordSysId = fd_data.trigger.current.sys_id;
var tableName = fd_data.trigger.current.sys_class_name;

var rec = new GlideRecord(tableName);
if(rec.get(triggerRecordSysId)){
var assignment_group = '';
var state = '';
var currentDate = new GlideDateTime();
gs.info("%%%Current Date is"+ currentDate);
var effectiveDate = new GlideDateTime(rec.variables.effective_date); // give your variable name here
gs.info("%%%Effective Date is"+ effectiveDate);