TransWikia.com

Server Side Javascript results in "Bad Request" in a CloudPage

Salesforce Asked on December 22, 2021

I’m running into an issue that I don’t think is a SSJS timeout issue. The default timeout is 30 minutes, and I end up getting "Bad Request" after about 4 minutes.

Basically, I have some code that is iterating through 988 records. The code is performing some math/trig operations to calculate the differences between two locations given the latitude and longitude. When I get the "Bad Request" message, it’s about 4 minutes into the code running and it completes around 700 of the 988 records. Has anyone ever ran into something like this before? Should the code run to its max timeout of 30 minutes IF there are no issues with the data being processed?

EDIT

What’s even MORE weird is that when the "Bad Request" message happens after about 4 minutes, I’ll rerun the code to pick up where it left off, and it will finish up completely fine which tells me that there really is no issue with the data. I have exceptions being output to the browser as you can see, but the only thing that outputs is "Bad Request".

Here’s the code:

    <script type="text/javascript" runat="server">
  Platform.Load("core","1.1.1");
  var debug = true;

  
  try {

    var subscribersDE = DataExtension.Init("Subscribers");
    var locationsDE = DataExtension.Init("GroceryStores");
    
    var subscribersFilter = {
            LeftOperand: {
              Property: "location1Name",
              SimpleOperator: "isNull"
            },
            LogicalOperator: "OR",
            RightOperand: {
              Property: "location1Name",
              SimpleOperator: "equals",
              Value: " "
            }
          }

    // filteredSubscribers is the potential subset of subscribers to operate on
    // if a record already has "closest location" data, we don't want to operate
    // on them.
    var filteredSubscribers = subscribersDE.Rows.Retrieve(subscribersFilter);
    
Write("filteredSubscribers count = " + filteredSubscribers.length + "</br>");
// currently times out after 300 or so subscriber count below only for CloudPages
// which is why I set it to iterate to 100
    
    for (var i = 0; i < filteredSubscribers.length; i++)
    {
      // iterate through subscribers that do not have location1Name value
      var sub = filteredSubscribers[i];
      var subKey = sub["SubscriberKey"];
      var subLat = sub["Latitude"];
      var subLong = sub["Longitude"];
      var temp = [];
      
Write("subscriber iteration # " + i + "</br>");      
      
      var locationsFilter = {
        LeftOperand: {
          Property: "Latitude",
          SimpleOperator: "greaterThan",
          Value: subLat-0.5
        },
        LogicalOperator: "AND",
        RightOperand: {
          Property: "Latitude",
          SimpleOperator: "lessThan",
          Value: subLat+0.5
        }
      }

      // retrieve function only returns max 2500 records, so we need to limit to less than that
      var locationsDERows = locationsDE.Rows.Retrieve(locationsFilter);
      
//Write(Stringify(locationsDERows));
      
Write("locationsDERows Count = " + locationsDERows.length + "</br>");
      
      for (var j = 0; j < locationsDERows.length; j++)
      {
        var loc = locationsDERows[j];
        var locLat = loc["Latitude"];
        var locLong = loc["Longitude"];

        var R = 6371000; // radius of Earth in meters
        var φ1 = subLat * Math.PI/180; // φ, λ in radians
        var φ2 = locLat * Math.PI/180;
        var Δφ = (locLat-subLat) * Math.PI/180;
        var Δλ = (locLong-subLong) * Math.PI/180;

        var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
                  Math.cos(φ1) * Math.cos(φ2) *
                  Math.sin(Δλ/2) * Math.sin(Δλ/2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

        var d = R * c; // in meters
        var distanceInMiles = d * 0.000621371;  
        
        var obj = {};
        
        obj['distance'] = distanceInMiles;
        obj['storeName'] = loc['StoreName'];
        obj['address'] = loc['Address'];
        obj['city'] = loc['City'];
        obj['state'] = loc['State'];
        obj['zip'] = loc['Zip'];
        
        temp.push(obj); 
      }

      temp.sort(function (a, b) {
        return a.distance - b.distance;
      });

  
      
      subscribersDE.Rows.Update({
        'location1Name':temp[0].storeName + " " + temp[0].address + " " + temp[0].city + ", " + temp[0].state + " " + temp[0].zip,
        'location1Distance':temp[0].distance,
        'location2Name':temp[1].storeName + " " + temp[1].address + " " + temp[1].city + ", " + temp[1].state + " " + temp[1].zip,
        'location2Distance':temp[1].distance,
        'location3Name':temp[2].storeName + " " + temp[2].address + " " + temp[2].city + ", " + temp[2].state + " " + temp[2].zip,
        'location3Distance':temp[2].distance
      },['SubscriberKey'],[subKey]);
    }
    
  }
  catch(e) {
    var errorMsg = Stringify(e)
    Variable.SetValue("@err",errorMsg);
    Write(errorMsg);
  }
</script>

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP