0

Follow-up to a similar question. I'd like to be able to access the rows[i] by field name, and saw something similar in another example, but I couldn't get it to work. In the code below, SubscriberKey1 works fine, but SubscriberKey2 comes back as Undefined. Can someone help me understand why?

<script runat="server">

Platform.Load("Core","1.1.1");
  
var rr = Platform.Function.CreateObject("RetrieveRequest");
Platform.Function.SetObjectProperty(rr, "ObjectType", "DataExtensionObject[MyDataExtension]");
Platform.Function.AddObjectArrayItem(rr, "Properties", "SubscriberKey");

do {
    var results = [0,0];
    var rows = Platform.Function.InvokeRetrieve(rr, results);
    var runstatus = results[0];
    var requestId = results[1];
  
    if (rows != null) {
        for (i=0; i < rows.length; i++) {
            var SubscriberKey1 = rows[i].Properties[0].Value;
            var SubscriberKey2 = rows[i].SubscriberKey;
            Write(i + ": " + SubscriberKey1 + " <br/>");
            Write(i + ": " + SubscriberKey2 + " <br/>");
        } 
    } 

    rr.ContinueRequest = requestId;


} while (runstatus == "MoreDataAvailable");
  
  
</script>
3

You need to understand the data structure you are dealing with, and how parsing it works.

The reason is that

Platform.Function.AddObjectArrayItem(rr, "Properties", "SubscriberKey");

, more specifically "AddObjectArrayItem" ADDS Subscriber Key, which is stored in the data extension as an OBJECT (a key:value pair), to the Properties ARRAY as an ITEM.

So you basically have to dissect this data structure piece by piece.

  1. An array (Properties)
  2. With the dataExtensionField Subscriberkey (represented as an Object) inside as item
  3. whose Value you want

An array can be visualized as a "list" of items enclosed in []: ["a","b","c"]. In your example, you have a very short Array of properties items:

[{Subscriberkey:12345}]

Which is DEFINED AS AN ARRAY, even if it's only one item.

These items have no name assigned, they simply have numbers that start at 0. So in the ["a","b","c"] Array, to get to "a", you use Properties[0].

Regardless of length, the computer treats your very short, one-item array called Properties like it were a "list", and you have to get items from the list to work with them.

[{Subscriberkey:12345},(here could be more),(even more)]

Which is why Properties[0] is needed.

Only NOW you have:

{Subscriberkey:12345}

as an OBJECT to work with.

An object is a key:value pair enclosed in {}: {"key":"value"}

In an object, there are two things that you can Retrieve: Name (here "SubscriberKey") Value (here "12345")

If you want 12345, the value, you tell the machine to "go into the first Array item and return its value":

Properties[0].Value

That is why the line that works, works:

var SubscriberKey1 = rows[i].Properties[0].Value;

It does exactly that.

Btw, you could do the same with Name.

The line that does not work:

var SubscriberKey2 = rows[i].SubscriberKey;

...simply misses both important steps. It does not tell the computer where to go in the array, and even if it did, it doesn't tell it what to take out of the object it would find.

1
  • Thank you! I don't have enough reputation to up-vote the answer. The reason I thought we could do the direct reference was because of another post, here, which seemed to be doing exactly that. link – Charlie Heitzig Apr 9 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.