0

I'm trying to output message if there is an error and i have very little knowledge in javascript in general.

Could I get some feedback on if the following would work or how I would go about :

  • If empty parameters, output "Sorry, there was an error"
  • If there is an error, output "Sorry, there was an error"

Thank you in advance!

<script runat="server">

Platform.Load("core","1.1.5");

try {
</script>
%%[

/* Retrieve Parameters */
SET @subscriberkey = _subscriberkey
SET @jobid = jobID
SET @firstname = RequestParameter("FirstName")
SET @email = RequrestParameter("Email")

/* Handle Empty Parameters */
IF EMPTY(@subscriberkey) OR EMPTY(@jobid) OR EMPTY(@firstname) OR EMPTY(@email) OR @subscriberkey =='' OR @jobid == '' OR @listid == '' OR @Email == ''THEN
  SET @message = "Sorry, there was an error."
ENDIF

/*Lookup DE and Update if doesn't exist */
SET @Rows = LookupRows('DEName', "SubscriberKey", @subscriberkey)

IF RowCount(Rows) > 0 THEN 
   SET @message = "You've already registered."
ELSE
   SET InsertDE = InsertData('DEName','SubscriberKey', @subscriberkey, 'Email',@Email)
   SET @message = "Thanks for registering"
ENDIF

]%%

%%=v(@message)=%%

<script runat="server">
} catch (e) {
  Write("<br>" + Stringify(e))
}

</script>
2
  • change your code 'RowCount(Rows) > 0' to 'RowCount(@Rows) > 0'
    – Abhishek
    Nov 13, 2019 at 11:24
  • You are doing a redundant check, as EMPTY() function both checks for the variable being null as well as an empty string. Nov 13, 2019 at 11:38

1 Answer 1

2

Here are a couple changes (based on comments by @Abhishek and @LukasLunow) as well as a few other things I noticed. This should get you there:

<script runat="server">

Platform.Load("core","1.1.5"); // Not necessary if just doing try/catch. Unnecessary processing

try {
</script>
%%[

/* Retrieve Parameters */
SET @subscriberkey = AttributeValue("_subscriberkey") /* Set to AttributeValue for null handling */
SET @jobid = AttributeValue("jobID") /* Set to AttributeValue for null handling */
SET @firstname = RequestParameter("FirstName")
SET @email = RequestParameter("Email") /*Spelling mistake on function. changed from RequrestParameter to RequestParameter */

/* Handle Empty Parameters */
IF EMPTY(@subscriberkey) OR EMPTY(@jobid) OR EMPTY(@firstname) OR EMPTY(@email) THEN /* Removed unncessary conditions */
  SET @message = "Sorry, there was an error."
ENDIF

/*Lookup DE and Update if doesn't exist */
SET @Rows = LookupRows('DEName', "SubscriberKey", @subscriberkey)

IF RowCount(@Rows) > 0 THEN   /* Added @ to AMPscript Variable Rows */
   SET @message = "You've already registered."
ELSE
   SET @InsertDE = InsertData('DEName','SubscriberKey', @subscriberkey, 'Email', @Email) /* Added @ to AMPScript Var InsertDE */
   SET @message = "Thanks for registering"
ENDIF

]%%

%%=v(@message)=%%

<script runat="server">
} catch (e) {
  //Write("<br>" + Stringify(e)) //This will display the error code/description if it fails, not your custom message
  Write("Sorry, there was an error.") //This will display your custom message if it fails
}

</script>

List of recommended changes:

  1. Remove Core Library call (if you are just using SSJS for try/catch)
  2. Utilize AttributeValue() on personalization strings for null handling (mostly just personal preference, not really necessary)
  3. Spelling mistake on RequestParameter function when setting @email
  4. Remove unnecessary conditions for Empty Parameters IF statement
  5. Added @ to AMPscript var 'Rows'
  6. Added @ to AMPscript var 'InsertDE'
  7. Provided SSJS Write function with your custom message instead of the error details inside of catch
3
  • Assuming i would insert this ssjs try catch in an email without requestparameter stuff, where will Write(...) inside the catch function output the error message? Do i still have to call RaiseError(....,1) inside the catch block when i would like to skip the subscriber? Should attributeValue(...) be used for every dataextension column value i would like to get? If country would be a dataextension value would i write it as set @country = [country] or something with attributevalue? Can i still InsertData inside the catch block when ending and starting the script tag again? Nov 13, 2019 at 15:01
  • Just some comment to your code additionally: I found out that you need to Load Platform Libraries to be able to use the Write function like you did. And you missed the ; after the ending of the write command. Still have a question: What will the error actually output? When i create errors in my ampscript on purpose (like missing ENDIFs) the cloudpage created with the code won't even render. Nov 14, 2019 at 14:12
  • Thanks @JohannesSchapdick - I will try to get you more info on those questions shortly and thanks for the update on your findings. Nov 14, 2019 at 20:31

You must log in to answer this question.

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