0

I've created a custom form on a cloudpage with a handler to insert a phone number into a data extension to honor a mobile unsubscribe. Once you enter the phonenumber and press submit, you are redirected to a confirmation page.

My Data extension has a single column called MobileNumber, which is the primarykey.

Everything works, except if you try to enter a duplicate number then you get the 500 error page. I'm guessing that this is due to a primaykey violation.

Is there a way that I can display a message that says, "phone number already exists" opposed to the 500 error page?

 

 SET @MobileNumber = RequestParameter("MobileNumber")

  InsertData("unsubs", "MobileNumber", @MobileNumber)

Redirect("cloudpageurl goes here")
 
 ELSE
  OUTPUTLINE(CONCAT("Wrong reCAPTCHA: ", @Response))
 ENDIF


2 Answers 2

1

Unfortunately, you cannot "convert" 500 Error to "Phone already exists" message. Since the InsertData function would be already executed and thus always throw an error when the MobileNumber already exists in the DE.
But you can add another function to preliminary check if the same MobileNumber already exists in the Data Extension via the Lookup function:

%%[

SET @MobileNumber = RequestParameter("MobileNumber")

SET @MobileNumberLookup = Lookup("unsubs", "MobileNumber", "MobileNumber", @MobileNumber)

IF EMPTY (@MobileNumberLookup) THEN
   InsertData("unsubs", "MobileNumber", @MobileNumber)
ELSE
   OUTPUTLINE(CONCAT("Phone number already exists: ", @MobileNumber))
ENDIF

]%%
1
  • Thanks, I'll give both solutions a try and see which works best. I think changing to UpsertData might be the "quickfix" until I roll something more permanent out.
    – lyslexic
    Mar 23, 2023 at 9:04
1

You have to use the [UpsertData] function instead of InsertData, you could add a timestamp field on your DE.

So in your case, it will be :

    set @updatedDate = Now()
    upsertData("unsubs", "MobileNumber", @MobileNumber,"Updated", @updatedDate))

To display error message, enclose your ampscript in ssjs try and catch block like below:

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

%%[ your AMPscript block goes here  ]%%

<script runat="server">
}catch(e){
 Write(Stringify(e));
}
</script>

As a best practise, also log your error messages in a data extension.

2
  • There is only 1 field in the Data Extension, thus UpsertData function won't give a difference except for preventing the page from throwing an error.
    – Niko
    Mar 23, 2023 at 8:49
  • Sorry I missed that information, maybe you can have a timestamp field created, so that you can upsert based on this. Editing my answer Mar 23, 2023 at 8:50

You must log in to answer this question.

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