Hi All, Can we access/use Global Variable in MR sc...
# suitescript
s
Hi All, Can we access/use Global Variable in MR script. I need to access the global variable in summarize() function. (Note: the value for this global variable is set in getInputData() function). Is it really possible to access the global variable in all the phases of MR script, if so how? Provide some examples if any.
b
no global variables, do something like create a custom record
if you want to be risky, use the cache module and hope netsuite doesn't clear your cache
😨 1
s
Okay, Does Netsuite have any default time to clear away the cache ?
b
i wouldnt recommend cache module
👍 2
i mention it because others think its a workable solution
that said
Copy code
The maximum duration, in seconds, that a value retrieved by the loader can remain in the cache. Note that the value may be removed from the cache before the ttl limit is reached.

The minimum value is 300 (five minutes) and there is no maximum. The default ttl value is no limit.
Important

A cached value is not guaranteed to stay in the cache for the full duration of the ttl value. The ttl value represents the maximum time that the cached value may be stored.
a
@SMNCP I think we could use
const
not very sure as haven't used in map reduce
s
If you just want static global variables, then yes they can be defined in the script and will be available to all stages. Updates to variables in one stage will not be preserved in later stages, though, so you can't do dynamic globals without a custom record
e
One option would be to include the value that you want to be available in the data object you're returning from getInputData stage. In essence, it will be part of the each row/record passed to each stage.
b
i like that option if you only need to read the global and dont need to write to it again
e
The technique works well when combined with a custom record for logging what happens inside a Map/Reduce script. On the getInputData stage you can create the custom record, grab the id of the new record and pass it to each stage as part of the data object and access it at each stage to write back to the custom record to log whatever activity you need.
👍 1
j
I've also had to pass data from the getInputData stage to summarize stage. What I've done is create a custom and put the currently map reduce task's id into a field on the custom record in order to find it in the summarize stage. I didn't like the idea of passing it into the dataset because, while unlikely, I think it's still possible for every map or reduce stage to fail, in which your summarize stage output iterator will be empty.
Copy code
/**
 * Get the task id of the currently running map/reduce deployment instance.
 * i.e. what would be returned from N/task.create().sumbit()
 *
 * Rely on the fact that a script deployment can only have one running instance at a time.
 */
function getCurrentTaskId() {
	const deploymentId = runtime.getCurrentScript().deploymentId;
	const results = search.create({
		type: search.Type.SCHEDULED_SCRIPT_INSTANCE,
		filters: [
			['scriptdeployment.scriptid', 'is', deploymentId],
			'and',
			['status', 'anyof', 'PROCESSING']
		],
		columns: ['taskid']
	}).run().getRange({ start: 0, end: 1 });
	if (results.length === 0) {
		// should be impossible
		throw new Error('Failed to find the currently running script deployment instance.');
	}
	return results[0].getValue('taskid');
}