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);
}
Best Answer
From just having a quick look at your code it seems like a typo: instead of
data.lenght
you should trydata.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:An example of looping through retrieved data can be found in the marketing documentation about the LookupRows function.