Re run workflow timer script when variable updated

Ahmet1
Tera Expert

We have a timer script that waits 7 days before a date variable to run:

 

Workflow Timer:

var number_of_days = -7;

var time = (parseInt(gs.dateDiff(gs.nowDateTime(), current.variables.planned_start_date.getDisplayValue(), true), 10) + (number_of_days * 86400));
// Set 'answer' to the number of seconds this timer should wait
answer = time;

 

 

When the date variable is updated (planned_start_date), I want the timer script to run again.. It is a long shot probably but I found this code that updates the next_activity for that timer when variable is updated however the problem is that it will update next_action to the date from the updated variable date. What i want to check is if there is any way to add the workflow timer script somewhere in this code below where it is updating the next_action , to maybe run this timer script again?

 

In the code below, where it says " schedule.next_action = ritm.variables.due_date;" , I want schedule.next_action to be the new date calculated so if date variable field is updated to 30/07/2023, next_action should be 7 days before this date but I'm not sure how to script this calculation? 

 

(function executeRule(current, previous /*null when async*/) {
	//First, we have to get the context for the workflow running against current.
	var context = '';
	var wf = new GlideRecord('wf_context');
	wf.addQuery('id', current.sys_id);
	wf.query();
	while(wf.next()) {
		context = wf.getValue('sys_id');
	}
	

	//this is the name of your timer activity in the worklow - since you can have more than one. 
	var timer_name = 'wait_for_start';
	var doc_id = '';
	
	
	//Now we have to get the executing workflow activity records associated to the context.
	var wf_executing = new GlideRecord('wf_executing');
	wf_executing.addQuery('context', context);
	wf_executing.query();
	
	//Now we cycle through the activities to find the one we have named.
	while(wf_executing.next()){
		
		if(wf_executing.activity.name == timer_name) {
			doc_id = wf_executing.getValue('sys_id');
		}
		
	}
	
	//Now, we use everything we have learned to find the right entry in the scheduled job table.
	var schedule = new GlideRecord('sys_trigger');
	schedule.addQuery('document_key', doc_id );
	schedule.query();
	
	while(schedule.next()) {
		//and here comes the magic
		//update the next_action field using the value in the appointment_time variable.
		schedule.next_action = ritm.variables.due_date;
		schedule.update();
		
	}
	
	//Success!
	
})(current, previous);

 

 

2 ACCEPTED SOLUTIONS

DrewW
Mega Sage
Mega Sage

If you do not care about a schedule then this should be simple like the below.  I did not test but it gives you the basic idea.

 

var dt = new GlideDateTime();
dt.setValue(current.getDisplayValue("variables.myVar"));
dt.addDaysUTC(-7);
schedule.next_action = dt;

 

View solution in original post

Thanks for reply. This is the right track.. I have an issue when I put it into my code, it seems to be deleting the sys_trigger entry / skipping timer when I make the update , when the business rule runs.

This is how I've added it, towards the end. 

(function executeRule(current, previous /*null when async*/) {
	//First, we have to get the context for the workflow running against current.
	var context = '';
	var wf = new GlideRecord('wf_context');
	wf.addQuery('id', current.sys_id);
	wf.query();
	while(wf.next()) {
		context = wf.getValue('sys_id');
	}
	

	//this is the name of your timer activity in the worklow - since you can have more than one. 
	var timer_name = 'wait_for_start';
	var doc_id = '';
	
	
	//Now we have to get the executing workflow activity records associated to the context.
	var wf_executing = new GlideRecord('wf_executing');
	wf_executing.addQuery('context', context);
	wf_executing.query();
	
	//Now we cycle through the activities to find the one we have named.
	while(wf_executing.next()){
		
		if(wf_executing.activity.name == timer_name) {
			doc_id = wf_executing.getValue('sys_id');
		}
		
	}
	
	//Now, we use everything we have learned to find the right entry in the scheduled job table.
	var schedule = new GlideRecord('sys_trigger');
	schedule.addQuery('document_key', doc_id );
	schedule.query();
	
	var dt = new GlideDateTime();
	
	while(schedule.next()) {
		//and here comes the magic
	dt.setValue(current.getDisplayValue("variables.planned_start_date"));
	dt.addDaysUTC(-7);
	schedule.next_action = dt;
	schedule.update();
		
	}
	
	//Success!
	
})(current, previous);

View solution in original post

5 REPLIES 5

DrewW
Mega Sage
Mega Sage

If you do not care about a schedule then this should be simple like the below.  I did not test but it gives you the basic idea.

 

var dt = new GlideDateTime();
dt.setValue(current.getDisplayValue("variables.myVar"));
dt.addDaysUTC(-7);
schedule.next_action = dt;

 

Thanks for reply. This is the right track.. I have an issue when I put it into my code, it seems to be deleting the sys_trigger entry / skipping timer when I make the update , when the business rule runs.

This is how I've added it, towards the end. 

(function executeRule(current, previous /*null when async*/) {
	//First, we have to get the context for the workflow running against current.
	var context = '';
	var wf = new GlideRecord('wf_context');
	wf.addQuery('id', current.sys_id);
	wf.query();
	while(wf.next()) {
		context = wf.getValue('sys_id');
	}
	

	//this is the name of your timer activity in the worklow - since you can have more than one. 
	var timer_name = 'wait_for_start';
	var doc_id = '';
	
	
	//Now we have to get the executing workflow activity records associated to the context.
	var wf_executing = new GlideRecord('wf_executing');
	wf_executing.addQuery('context', context);
	wf_executing.query();
	
	//Now we cycle through the activities to find the one we have named.
	while(wf_executing.next()){
		
		if(wf_executing.activity.name == timer_name) {
			doc_id = wf_executing.getValue('sys_id');
		}
		
	}
	
	//Now, we use everything we have learned to find the right entry in the scheduled job table.
	var schedule = new GlideRecord('sys_trigger');
	schedule.addQuery('document_key', doc_id );
	schedule.query();
	
	var dt = new GlideDateTime();
	
	while(schedule.next()) {
		//and here comes the magic
	dt.setValue(current.getDisplayValue("variables.planned_start_date"));
	dt.addDaysUTC(-7);
	schedule.next_action = dt;
	schedule.update();
		
	}
	
	//Success!
	
})(current, previous);

I just noticed it should be

dt.setDisplayValue(current.getDisplayValue("variables.planned_start_date"));

Not the setValue.

 

I have not done this kind of thing this way before.  So unless the date is somehow messing it up I'm not sure what the issue is based on your last update.

 

The way I have done this is to have a path that bypasses the timer if a condition is met and if that condition is the date changed then I have it go back around again so it starts up the timer again with a new end time.

This is working and definitely does what I need it to do! Thanks allot