0

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>
5
  • if you put a try/catch block around the redirect, what error is returned? Commented Sep 14, 2020 at 12:21
  • Why are you writing output to the page and immediately redirecting? Might be easier to troubleshoot if you set some flags on error conditions and redirect at the end if the flag is set -- that way you can easily disable the redirect to view the output when debugging. Commented Sep 14, 2020 at 12:23
  • 1
    @AdamSpriggs - I indeed think the issue is with including text output before the Redirect call - as I explain in my updated answer. Commented Sep 14, 2020 at 13:41
  • If you notice, the URL you have in the If(ContactId...) is different than what is set as default above that. Could the url be malformed or relative instead of absolute? (Also as you have it in this example, I think the default one would fail as it needs the protocol in order to work) Commented Sep 14, 2020 at 13:44
  • Thanks for everyone's answers. I have made a lot of changes, and this actually works now, and I'm not really sure why...
    – yoni349
    Commented Sep 15, 2020 at 6:51

1 Answer 1

0

You forgot to set a semicolon in the line, where you define errorURL:

if(ContactId == 'null'){
    var errorURL = 'https://errorURL.com';
    Redirect(errorURL,false);
}

Also, I will suggest to do this test in a slightly better way, to ensure it actually does what you expect. In this case, it actually checks if your contact ID has the string value of 'null'. I expect this is what you need instead:

if(ContactId == null || ContactId == ''){
    var errorURL = 'https://errorURL.com';
    Redirect(errorURL,false);
}

Another reason for the error (which is more plausible in your case) is printing any output before the Redirect function. As the redirect function alters the header of the Cloud Page (responding with code 302 and the destination URL set in the Location header), outputting anything prior to the Redirect function results in an error, as the headers are set on the first line of code with output in it (e.g. utilising Write function).

So remove any usage of Write in your SSJS which might interfere with your Redirect, and also ensure no output is generated above the opening <script> tag.

4
  • thanks but it doesn't work. actually i don't think you really need semicolon.
    – yoni349
    Commented Sep 14, 2020 at 10:29
  • 1
    @yoni349 - updated with what I think is more plausible cause of your error Commented Sep 14, 2020 at 13:31
  • Did my latest suggestion help you, @yoni349? Commented Sep 15, 2020 at 7:27
  • It actually worked without removing the "write". So guess that wasn't the issue here. really appreciate your help though. By the time to got the see your inputs i made so many changes and lost track about what actually made it work. I think it got to do with MC executing the catch even though there are not any errors. As mentioned here howtosfmc.com/posts/the-catches-of-try-catch-in-sfmc-ssjs. I think it causes some executions before time.
    – yoni349
    Commented Sep 15, 2020 at 7:47

You must log in to answer this question.

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