4 Replies Latest reply on May 29, 2018 1:00 PM by stewarts16448458

    How to access data elements in custom code

    christophers7554118 Level 1

      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. Re: How to access data elements in custom code
          stewarts16448458 Level 4

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

          3 people found this helpful
          • 2. Re: How to access data elements in custom code
            stewarts16448458 Level 4

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

            2 people found this helpful
            • 3. Re: How to access data elements in custom code
              christophers7554118 Level 1

              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?

              • 4. Re: How to access data elements in custom code
                stewarts16448458 Level 4

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