How to access data elements in custom code

Accepted Solutions (1)

Accepted Solutions (1)

StewSchilling

MVP

25-05-2018

You bet you can.

var bkprop4 = _satellite.getVar("myDataElement");

The % signs are just for use in the UI.  They can also be used in HTML tags - I'm pretty sure that they trigger replacement during the rendering process.

You can also use _satellite.setVar("variableName", value) in your custom code, but only for new data elements.  You cannot change the value of a UI created data element using setVar.   I like to use a standard naming convention when I dynamically create data elements using _satellite.setVar.  This way I have no chance of conflicting with UI data elements.

One more point.  I often will create a data element in a rule's custom code and then reference it from the UI in an action.  When doing this, you need to manually enter it in the UI field (using the %name% notation as normal).

Answers (3)

Answers (3)

StewSchilling

MVP

29-05-2018

No issues at all (as long as you don't intend to use that data element in the Launch UI.

On that note, I've been wanting to try the new Core Extension Event Type of (custom code) with an AJAX success handler.  This event type has the guidance, `The code you provide in the editor will be run as soon as the library is loaded. Inside your code, call trigger() whenever you would like the rule to fire.`

It seems that you could put your AJAX event listener as custom code and when you catch the event (or events) that you care about, call trigger().  You can also put a JSON object as a parameter in the trigger call, i.e. `trigger({key1:value1, key2:value2})`

StewSchilling

MVP

25-05-2018

One more thing that might blow your mind is that although the UI expects a data element to return a string, you can store anything in them (Objects, Functions, Arrays, Strings, Numbers, Booleans). 

As an example:

_satellite.setVar( "dynPersonObj", {firstName: "stew", lastName: "schilling", age: "old"} );

_satellite.logger.log( "AGE is ", _satellite.getVar("dynPersonObj")[age] );

Maybe that's more than you wanted to know...

christopher2151

29-05-2018

stewarts16448458​ Thank you for the detailed response. Could you see an issue with creating a data element inside Launch that retrieves an JSON object from an Ajax call?