Trigger Subflow for Scheduled Job

neil_b
Tera Guru

Hello ServiceNow Community!

 

I am trying to trigger a subflow on my scheduled job that's supposed to run Mondays & Wednesdays. Here is my condition: 

answer = false;
var gdt = new Date();
var day = gdt.getDay();
if(day != 0 && day != 2 && day != 4 && day != 5 && day != 6) {
answer = true;
} 

This works fine because when I use gs.info, day 3 returns today and answer is in fact true.

 

My script to execute is as follows: 

(function() {
	try {
		var result = sn_fd.FlowAPI
		.getRunner()
		.subflow('global.approval_reminder')
		.inForeground()
		.run();
	} catch (ex) {
		var message = ex.getMessage();
		gs.error(message);
	}
})();

 

When I execute this job, the subflow is not being triggered. What am I doing incorrectly?

1 ACCEPTED SOLUTION

@neil_b 

don't use self invoking function, simply use normal function

runJob();

function runJob(){

// your code

}

Did you try running your script in background script and see if subflow is getting triggered?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

@neil_b 

is your instance slow because sometimes schedule job takes time to run as system will pick it based on queue mechanism?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

No, there's nothing else running on the instance. I did finally get it to work. 

 

This is my final code snippet -- I just needed to return the answer after the last curly bracket.

answer = false;
var gdt = new Date();
var day = gdt.getDay();
if(day != 0 && day != 2 && day != 3 && day != 5 && day != 6) {
answer = true;
}
answer;

 

runJob();
function runJob(){
		try {
		var result = sn_fd.FlowAPI
		.getRunner()
		.subflow('global.approval_reminder')
		.inBackground()
		.run();
	} catch (ex) {
		var message = ex.getMessage();
		gs.error(message);
	}
}

Your suggestion of the function helped. I will mark yours as the solution. Thanks!