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

Ankur Bawiskar
Tera Patron
Tera Patron

@neil_b 

did you add gs.info() and see in that script?

this didn't come?

	gs.info('nb did this run?');

Did you test your subflow actually works and no error is thrown?

try to check this

(function() {
	try {
                gs.info('Before triggering subflow');
		var result = sn_fd.FlowAPI
		.getRunner()
		.subflow('global.approval_reminder')
		.inForeground()
		.run();
	gs.info('nb did this run?');
	} catch (ex) {
		var message = ex.getMessage();
		gs.error(message);
	}
})();

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

Hi @Ankur Bawiskar I did try adding the gs.info in the beginning before triggering the subflow and after the execution and I didn't see 'before triggering subflow' or 'nb did this run?' in the log, which means the script is not being executed. The subflow does work just fine if executed it manually.

 

What can I do to resolve this? Is my conditional script written incorrectly? I even added gs.info logs in my conditional script:

gs.info('nb - gdt is ' + gdt);
gs.info('nb - day is ' + day);
gs.info('nb - answer is ' + answer);

and these are the results:results.png

 

@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

@Ankur Bawiskar I did what you suggested. Here is my new scheduled script:

runJob();
function runJob(){
		try {
	gs.info('did the script even start?');
		var result = sn_fd.FlowAPI
		.getRunner()
		.subflow('global.approval_reminder')
		.inForeground()
		.run();
	gs.info('did this run?');
	} catch (ex) {
		var message = ex.getMessage();
		gs.error(message);
	}
}

It still did not execute the job.

 

I did try running this exact script using a Background Script and it works flawlessly using a background script. 

 

I am not sure why it won't execute in my scheduled job.