0

I am trying to retrieve rows from a data extension and post them to and REST end point. I am ok with retrieving the data, but struggling to construct the loops for the API call. This is my code.

<script runat="server">

Platform.Load("Core","1");

var EventDE = DataExtension.Init("EnBWEventSource"); //Inititialize data extension 
var filter = {Property:"SendLetter", SimpleOperator:"equals", Value:"True"}; // Set filter to flag value
var data = EventDE.Rows.Retrieve(filter); // Retrieve all values that match that DE description


// Write(Stringify(data));


for (var i = 0 ; i < data.lenght; i ++)

    var url = 'http://example.com/forms/myForm.html';
    var contentType = 'text/xml';
    var payload = '<test>test123</test>'; // How do I pay the headers, values and rows into this?
    var headerNames = ["MyTestHeader1", "MyTestHeader2"];
    var headerValues = ["MyTestValue1", "MyTestValue2"];
    var result = HTTP.Post(url, contentType, payload, headerNames, headerValues);


    Write(result.StatusCode + '<br>');
    Write(result.Response);

As always I will appreciate pointers on this.

UPDATE

I have updated the script but it still seems not to be working. Here is the script below:

<script runat="server">

Platform.Load("Core","1");

var EventDE = DataExtension.Init("EnBWEventSource"); //Inititialize data extension 
var filter = {Property:"Message", SimpleOperator:"equals", Value:"True"}; // Set filter to flag value
var data = EventDE.Rows.Retrieve(filter); // Retrieve all values that match that DE description


// Write(Stringify(data));


for (var i = 0 ; i < data.length ; i ++) {

    var url = 'http://example.com/mock/endPoint.html';
    var contentType = 'application/json';
    var payload = {
        "key1" : data["key1"],
        "key2": data["key2"],
        "key3": data["key3"],
        "key4": data["key4"]

    };
    var headerNames = ["Authorization"];
    var headerValues = ["{{accessToken}}"];
    var result = HTTP.Post(url, contentType, payload, headerNames, headerValues);


    Write(result.StatusCode + '<br>');
    Write(result.Response); 
}

1 Answer 1

3

From just having a quick look at your code it seems like a typo: instead of data.lenght you should try data.length. Additionally you need to add curly braces to your for-loop, so all the code that should be performed multiple times is inside this block, so your for-loop should look like this:

for (var i = 0; i < data.length; i++) {
    // your code accessing data[i]["yourcolumn"]
}

An example of looping through retrieved data can be found in the marketing documentation about the LookupRows function.

<script runat="server">
     var dataRows = Platform.Function.LookupRows('CustomerData','Company','exampleCompany');
     if(dataRows && dataRows.length > 0) {
          for(var i=0; i<dataRows.length; i++) {
               Platform.Response.Write(dataRows[i]["Email"]);
          }
     }
</script>
8
  • Do I need to use the lookup rows function or is it just an example of the loop? Can I loop directly from the data object?
    – 0xsegfault
    Commented May 16, 2017 at 10:31
  • That's just an example, but if retrieval using EventDE.Rows.Retrieve works and data is an array, you can also loop over data using the for-loop provided in the first example of my answer. Commented May 16, 2017 at 11:11
  • Still stuck on this , I have updated my question. Am I calling the payload wrong?
    – 0xsegfault
    Commented May 16, 2017 at 12:46
  • You need to use the index i from the loop to access the correct position of your data-array and furthermore i guess the space between the i and the ++ in the loop need to be removed. So the loop would be for (var i = 0 ; i < data.length ; i++) { and building the payload would be done accessing data like this: data[i]["key1"] Commented May 16, 2017 at 12:49
  • Thanks!! Last do I need the Write(Stringify(data)) it seems to be commented out
    – 0xsegfault
    Commented May 16, 2017 at 12:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .