The Code Snippet not trigger the flow to every look up record

Bird1
Mega Sage

Hi

 

Need suggestion. I do have a scheduled job which will run on specific time.

 

  • The script will look up the RITM records on specific catalog item with 'Pending' state.
  • It will glide date and compare to the 'access_granted_date' variable to check the expired date
    • If within last 10 days (>= 170 && < 180), it will add the comment to RITM record.
    • If completed 180 days, it will trigger the sub-flow.

The problem is

  • In each scheduled job run, if there's no any RITM which completed 180 days, the while loop works smoothly to add comment to every look up records
  • But if in each scheduled job run, there's any record which already completed 180 days, it will trigger the subflow to only that first record and stop everything (the rest of look up record will not trigger).

 

Anyone can suggest for the solution? I need the sub-flow to be triggerable to every look up records in while loop.

 

Thanks.

 

 

 

 

var gdt = new GlideDate();

var ritm = new GlideRecord('sc_req_item');
ritm.addEncodedQuery('active=true^cat_item=01c8b2761b41f51037e598aebd4bcb30^state=-5^variables.995c43be1b81f51037e598aebd4bcb0dISNOTEMPTY'); // MX request access printing with state = Pending
ritm.query(); 
while (ritm.next()){
var date = GlideDateTime.subtract(new GlideDateTime(ritm.variables.access_granted_date),new GlideDateTime(gdt)); // Date difference calculation (Today - Access granted date)
var datediff = date.getDayPart(); // Get day difference
var remaineddate = 180 - datediff; // Get remained day
if (datediff >= 170 && datediff < 180) { // Witnin last 10 days

	ritm.comments = "Your " +ritm.variables.select_your_printer + " printer access will be expired in the next " +remaineddate + " day(s). Please submit request here: " + '[code]<a href="https://aligntech.service-now.com/esc?id=sc_cat_item&table=sc_cat_item&sys_id=01c8b2761b41f51037e598aebd4bcb30" target="_blank">MX Request access to printing</a>[/code]' + " to extend access another 180 days.";
	ritm.update();
}
 else if (datediff >= 180){  // Complete 180 days

(function() {

	try {
		var inputs = {};
		inputs['ritm'] = ritm; // GlideRecord of table: sc_req_item 
		inputs['user_id'] = ritm.requested_for.user_name; // String 

		// Start Asynchronously: Uncomment to run in background. Code snippet will not have access to outputs.
		// sn_fd.FlowAPI.getRunner().subflow('global.remove_access__mx_request_access_to_printing_after_180_days').inBackground().withInputs(inputs).run();

		// Execute Synchronously: Run in foreground. Code snippet has access to outputs.
		var result = sn_fd.FlowAPI.getRunner().subflow('global.remove_access__mx_request_access_to_printing_after_180_days').inForeground().withInputs(inputs).run();
		var outputs = result.getOutputs();

		// Current subflow has no outputs defined.		
	} catch (ex) {
		var message = ex.getMessage();
		gs.error(message);
	}


})();
}
}
1 REPLY 1

Sandeep Rajput
Tera Patron
Tera Patron

@Bird1 Instead of triggering the flow inside the while loop, you should consider replacing your GlideRecord query in script with a Look up Records action inside your flow.