Created
April 8, 2019 18:25
-
-
Save thisnameissoclever/ea89db38ee6d4801ed5fdc04a38d7c0a to your computer and use it in GitHub Desktop.
Event-Driven Recursion - http://recursion.snc.guru/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Does the work on each loop. | |
* @param {Number} [limit=10] - The number of records to process per loop. | |
* @param {Number} [currentNumber=0] - The number of records that have been processed so far, by all previous loops. | |
*/ | |
function eventWrapper(limit, currentNumber) { | |
var EVENT_NAME = 'EVENT.NAME.HERE'; //todo: Update this to the name of the event you've created. | |
var TABLE_NAME = 'TABLE_NAME_HERE'; //todo: Update this to the name of the table containing the records you're processing | |
var QUERY = 'some_query=here'; //todo: Put your query here | |
var gr = new GlideRecord(TABLE_NAME); | |
//Set default values | |
limit = Number(limit) > 0 ? Number(limit) : 10; //Default: 10 | |
currentNumber = currentNumber > 0 ? currentNumber : 0; //Default: 0 | |
gr.setLimit(limit); | |
gr.addQuery(QUERY); | |
gr.query(); | |
if (!gr.hasNext()) { | |
gs.log('EDR: [Script name here] completed with ' + currentNumber + | |
' total records processed.'); | |
return; //No more records to process. Halt function, and loop. | |
} | |
while (gr.next()) { | |
/* | |
* Do something to a batch of records here, based on the limit parameter | |
*/ | |
currentNumber++; | |
} | |
//Log a progress update | |
gs.log('EDR: event wrapper processed ' + currentNumber + | |
' records so far. Triggering the next loop.'); | |
//Trigger the event again | |
gs.eventQueue(EVENT_NAME, gr, limit, currentNumber); | |
} | |
eventWrapper(event.parm1, event.parm2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment