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")
  OUTPUTLINE(CONCAT("Wrong reCAPTCHA: ", @Response))

2 Answers 2


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)
   OUTPUTLINE(CONCAT("Phone number already exists: ", @MobileNumber))

  • 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
    Commented Mar 23, 2023 at 9:04

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">

%%[ your AMPscript block goes here  ]%%

<script runat="server">

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

  • 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
    Commented 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 Commented 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 .