Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

How to access data elements in custom code

christopher2151
Level 2
Level 2

Is there a way to reference the data elements I have created inside a snippet of custom code in the code editor?

var bkprop4 = "%myDataElement%";

1 Accepted Solution
StewSchilling
Correct answer by
Community Advisor
Community Advisor

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).

View solution in original post

5 Replies
StewSchilling
Correct answer by
Community Advisor
Community Advisor

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).

View solution in original post

extwebd96701854
Level 2
Level 2

Hi Stew, Could you please show us how to reference a data element created in UI to the one we are created in the custom code.

StewSchilling
Community Advisor
Community Advisor

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
Level 2
Level 2

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?

StewSchilling
Community Advisor
Community Advisor

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})`