I'm using a simple SSJS code to redirect according to an API call results. But I keep getting error 500.
Problematic code:
if(ContactId == 'null'){
var errorURL = 'https://errorURL.com'
Redirect(errorURL,false);
}
When I remove the Redirect(errorURL,false);
there is no error.
I even tried to copy this one from the MC example document:
Redirect("http://example.com/index.html",false);
And I still get an error.
Full code:
<script runat="server">
Platform.Load("core", "1.1.1");
var RowCountFlag = Platform.Variable.GetValue("@RowCountFlag");
if (RowCountFlag == 0) {
var firstName = Platform.Variable.GetValue("@firstName");
var lastName = Platform.Variable.GetValue("@lastName");
var email = Platform.Variable.GetValue("@email");
try {
var url = 'https://test33333?timeout=60';
var contentType = 'application/json';
var headerNames = ["Username", "Token"];
var headerValues = ["YYYYYYYYYYYYY", "XXXXXXXXXXXXxx"];
var payloadObj = {
"parameters":
[
{"Key": "Acc_firstName", "Value": firstName},
{"Key": "Acc_lastName", "Value": lastName},
{"Key": "emal", "Value": email},
]
}
var OptInDE = DataExtension.Init("my de");
var errorURL = 'www.erorURL.com'
var payloadString = Platform.Function.Stringify(payloadObj);
var result = HTTP.Post(url, contentType, payloadString, headerNames, headerValues);
if (result.StatusCode == 200) {
Variable.SetValue("@APIflag", '200')
var responseStr = Stringify(result.Response[0])
var cleanResponseStr = responseStr.replace(/[^\w\s]/gi, '')
var starts = 9 + cleanResponseStr.lastIndexOf("ContactId")
var ends = cleanResponseStr.indexOf("AssetId")
var ContactId = cleanResponseStr.slice(starts,ends)
Variable.SetValue("@ContactId", ContactId)
var rows = Platform.Function.InsertDE("my de",["Serial Number","Resquest","Respond", "ContactId"],[SN,Stringify(payloadObj),Stringify(result),ContactId]);
if(ContactId == 'null'){
var errorURL = 'https://errorURL.com'
Redirect(errorURL,false);
}
} else {
var rows = Platform.Function.InsertDE("my de",["Serial Number","Resquest","Respond"],[SN,Stringify(payloadObj),Stringify(result)]);
Redirect(errorURL, false);
}
} catch (e) {
var rows = Platform.Function.InsertDE("Name_Your_Dolphin_IConduct_Log",["Serial Number","Resquest","Respond"],[SN,Stringify(payloadObj),Stringify(result)]);
Write(Stringify("Err at API call: "+e));
Redirect(errorURL, false);
}
}
</script>