Is it possible to have a variable with global scop...
# suitescript
j
Is it possible to have a variable with global scope in a Custom Form Script in SS2, and have that variable be possible to be changed and referenced? I have some behaviour in my form that should call functions that are defined in my Custom Form Script, but it seems I need to call them via a
require
and as such, the script is loaded afresh and the value of the global variable is reset.
Example
Copy code
/
* cf_mycustomform_ss2.1.js
* @NApiVersion 2.1
* @NScriptType ClientScript
 */
define([], function() {

	var thing = 'fish';

	function pageInit() {

		// Should show 'fish';
		logValueOfThing();

	}

	function fieldChanged() {

		setThingToApple();
		
		// Should show 'apple';
		logValueOfThing();

	}

	function setThingToApple() {

		thing = 'apple';

	}

	function logValueOfThing() {

		console.log('Value of thing is now ' + thing);

	}
			
	
	return {
		
		fieldChanged: fieldChanged,
		pageInit: pageInit,
		setThingToApple: setThingToApple,
		logValueOfThing: logValueOfThing
		
	}
	
});
if I wanted something on the page to cause setThingToApple(); and logValueOfThing(); to be called, it seems I need to do a require to get to those, as they aren’t “defined”.
m
window.thing
b
there are no global variables defined
thing
is being accessed as part of the closure
m
What you wrote should work. But you don't need to return
setThingToApple
&
logValueOfThing
unless these methods need to be externally available.