0

I have created this SSJS Loop, but it will stop when a record is considered "bad". How do I skip over this record to continue the process?

<script runat="server">
Platform.Load("Core","1.1.1");
try{
var AddedToday= "1";
var OrdersDE = DataExtension.Init("DEName");
var rows = OrdersDE.Rows.Lookup(["AddedToday"], [AddedToday]);

if(rows.length >= 1) 
  {
  for (i = 0; i < rows.length; i++) 
      {
        StyleVAR = rows[i]["Style"];
        Style2VAR = rows[i]["Style2"];
        PreferredStoreVAR = rows[i]["PreferredStore"];
        EmailVAR = rows[i]["Email"]; 
    var url = 'https://[MID].collect.igodigital.com/c2/[MID]/track_page_view?payload={[payload]}}}';
    var response = HTTP.Get(url);
        var result = OrdersDE.Rows.Update({Profile:'test'}, ['Email'],[EmailVAR]);
    
      }
}
else {Write("No items to display");}

  }catch(e){
    var errorMsg = Stringify(e)
    Write("Oops, something went wrong!<br>Please contact info@email.com so we can help you finalize your request.");
    var targetDE = 'DENAME'; //pass external key of the target DE      
    var logDE = DataExtension.Init(targetDE);
    logDE.Rows.Add({ message: errorMsg, "place":"cloudpage"});
    
}
</script>
1
  • 2
    Can you give more info on how a record would be 'considered bad'? Is it that the row does not exist? The variables do not match expected values/types? Update/Lookup fails? ???? – Gortonington Dec 11 '20 at 13:24
0

You need to take a more granular approach with your try-catches as currently you have pretty much your whole code wrapped in one - an error on any level will stop it from executing.

You can do checks on two levels:

  1. See if the lookup found your source DE
  2. If the operation on each individual level was successful

Then the code could look like this:

<script runat="server">
Platform.Load("Core","1.1.1");

var rows = []; //

try{
    var AddedToday= "1";
    var OrdersDE = DataExtension.Init("DEName");
    rows = OrdersDE.Rows.Lookup(["AddedToday"], [AddedToday]);
}catch(e){
    var errorMsg = Stringify(e)
    Write("Oops, something went wrong!<br>Please contact info@email.com so we can help you finalize your request.");
    var targetDE = 'DENAME'; //pass external key of the target DE      
    var logDE = DataExtension.Init(targetDE);
    logDE.Rows.Add({ message: errorMsg, "place":"cloudpage"});
}

if(rows.length >= 1) 
  {
    for (i = 0; i < rows.length; i++) 
        {
        StyleVAR = rows[i]["Style"];
        Style2VAR = rows[i]["Style2"];
        PreferredStoreVAR = rows[i]["PreferredStore"];
        EmailVAR = rows[i]["Email"]; 

        try {
            var url = 'https://[MID].collect.igodigital.com/c2/[MID]/track_page_view?payload={[payload]}}}';
            var response = HTTP.Get(url);
            var result = OrdersDE.Rows.Update({Profile:'test'}, ['Email'],[EmailVAR]);    
        } catch (rowError) {
            //your code here
        }
    }
}
    else {Write("No items to display");
}

</script>

I've added var rows = []; at line 4, so that rows.length at row 18 does not throw and exception

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.