Global and Local Variables

Project: JWM – Phase 1
January 28, 2014
Project – JWM – Phase 1.5
January 31, 2014

Global and Local Variables

Issue:¬†I’ve got a function which uses oData to pull data from other parts of the system and then update values based on other options on a form and this works perfectly. My original version was to grab data from 1 form, set the values to fields on the new form, then do calculations based on those fields.

[Form 1] <-> oData Call <-> [Form 2: Store form 1 results] -> [Do logic on form 2]

I knew off the bat that this was slow. (Used less resources under certain circumstances, but not always) In an ideal world the oData would be called and all the logic would be based on the results returned.

The other problem that the results which are returned, need to be assigned to variables, which is absolutely fine. However, I need to be able to access the results across the other aspects of the system. Can’t do that with local variables!

<br />
var result = retrieved.results[0]["test_emailaddress"];<br />

That code is absolutely perfect if I’m going to do this:

<br />
var result = retrieved.results[0]["test_emailaddress"];<br />
Xrm.Page.getAttribute("test_email").setValue(result);<br />

However, if I need to access the result from somewhere else..

<br />
//debug, see if variable is available for use.<br />
function alert(){<br />
alert(result);<br />
}<br />

You’ll get an ‘undefined’ error. Which is fantastic. Now, the solution… DONT DECLARE IT!

<br />
result = retrieved.results[0]["test_emailaddress"];<br />

Now, calling the variable in another function works! Global variables are fun.

<br />
//debug, see if variable is available for use.<br />
function alert(){<br />
alert(result);<br />
}<br />

Note: Big chunks of code are left out, but you get the idea

Jason
Jason
I do things in the clloouuudddd