0

I try to build a profile center in marketing cloud cloud page, but got this

500 internal server error"

is there anything wrong with my amp script

my ampscript:

%%[
SET @contactId = Id

IF NOT EMPTY(@contactId) THEN

/* fetch data from Sales Cloud to show in the form */

SET @subscriberRows = RetrieveSalesforceObjects(
   "Contact",
   "Id,FirstName,LastName,Email,Phone,Birthdate,Preferences_Latestupdates__c,Preferences_Merchandise__c,Preferences_Community__c,Preferences_CorporateHospitality__c,Preferences_NRLW__c,HasOptedOutOfEmail",
   "Id", "=", @contactId
   )
   
  IF RowCount(@subscriberRows) > 0 THEN

  SET @row = row(@subscriberRows,1)
  SET @FirstName = field(@row,"FirstName")
  SET @LastName = field(@row,"LastName")
  SET @Email = field(@row,"Email")
  SET @Phone = field(@row,"Phone")
  SET @Birthdate = field(@row,"Birthdate")
  SET @Newsletter = field(@row,"Preferences_Latestupdates__c")
  SET @Events = field(@row,"Preferences_Merchandise__c")
  SET @Offers = field(@row,"Preferences_Community__c")
  SET @Hospi = field(@row,"Preferences_CorporateHospitality__c")
  SET @Nrlw = field(@row,"Preferences_NRLW__c")
  SET @HasOptedOutOfEmail = field(@row,"HasOptedOutOfEmail")

  
  IF @Newsletter == true THEN SET @Newsletterchk = "checked" ENDIF
  IF @Events == true THEN SET @Eventschk = "checked" ENDIF
  IF @Offers == true THEN SET @Offerschk = "checked" ENDIF
  IF @Hospi == true THEN SET @Hospichk = "checked" ENDIF
  IF @Nrlw == true THEN SET @Nrlwchk = "checked" ENDIF

ELSE
]%%

Sorry, something went wrong (no records found).

%%[
ENDIF

ELSE
]%%

Sorry, something went wrong (missing subscriber key).

%%[
ENDIF

IF RequestParameter("submitted") == true AND RequestParameter("unsub") == true THEN

  /* update contact in Sales Cloud */
  SET @updateRecord = UpdateSingleSalesforceObject(
      "Contact", @contactId,
      "Preferences_Latestupdates__c", "false",
      "Preferences_Merchandise__c", "false",
      "Preferences_Community__c", "false",
      "Preferences_CorporateHospitality__c", "false",
      "Preferences_NRLW__c", "false",
      "HasOptedOutOfEmail", "true"
     )  
  
  /* log unsubscribe event to mark as unsubscribed in All Subscribers */
  SET @reason = "Unsubscribed through custom subscription center"

  SET @lue = CreateObject("ExecuteRequest")
  SETObjectProperty(@lue,"Name","LogUnsubEvent")

  SET @lue_prop = CreateObject("APIProperty")
  SETObjectProperty(@lue_prop, "Name", "SubscriberKey")
  SETObjectProperty(@lue_prop, "Value", @contactId)
  AddObjectArrayItem(@lue, "Parameters", @lue_prop)

  SET @lue_prop = CreateObject("APIProperty")
  SETObjectProperty(@lue_prop, "Name", "Reason")
  SETObjectProperty(@lue_prop, "Value", @reason)
  AddObjectArrayItem(@lue, "Parameters", @lue_prop)

  SET @lue_statusCode = InvokeExecute(@lue, @overallStatus, @requestId)

ELSEIF RequestParameter("submitted") == true AND RequestParameter("sub") == true THEN

  /* update contact in Sales Cloud */
  SET @updateRecord = UpdateSingleSalesforceObject(
      "Contact", @contactId,
      "FirstName", RequestParameter("firstname"),
      "LastName", RequestParameter("lastname"),
      "Email", RequestParameter("email"),
      "Phone", RequestParameter("Phone"),
      "Birthdate", RequestParameter("Birthdate"),
      
      "Preferences_Latestupdates__c", "true",
      "Preferences_Merchandise__c", "true",
      "Preferences_Community__c", "true",
      "Preferences_CorporateHospitality__c", "true",
      "Preferences_NRLW__c", "true",
      "HasOptedOutOfEmail", "false"

     )
     
   /* set subscriber status to active in All Subscribers */  
   SET @email = RequestParameter("email")
   
   SET @Subscriber = CreateObject("Subscriber")
   SetObjectProperty(@Subscriber, "SubscriberKey", @contactId)
   SetObjectProperty(@Subscriber, "EmailAddress", @email)
   SetObjectProperty(@Subscriber, "Status", "Active" )
   SET @Status = InvokeUpdate(@Subscriber, @createErrDesc, @createErrNo, @createOpts)

   /* fetch updated data to show in the form */
   SET @subscriberRows = RetrieveSalesforceObjects(
   "Contact",
   "Id,FirstName,LastName,Email,Phone,Birthdate,Preferences_Latestupdates__c,Preferences_Merchandise__c,Preferences_Community__c,Preferences_CorporateHospitality__c,Preferences_NRLW__c,HasOptedOutOfEmail",
   "Id", "=", @contactId
   )

  SET @row = row(@subscriberRows,1)
  SET @FirstName = field(@row,"FirstName")
  SET @LastName = field(@row,"LastName")
  SET @Email = field(@row,"Email")
  SET @Phone = field(@row,"Phone")
  SET @Birthdate = field(@row,"Birthdate")
  SET @Newsletter = field(@row,"Preferences_Latestupdates__c")
  SET @Events = field(@row,"Preferences_Merchandise__c")
  SET @Offers = field(@row,"Preferences_Community__c")
  SET @Hospi = field(@row,"Preferences_CorporateHospitality__c")
  SET @Nrlw = field(@row,"Preferences_NRLW__c")
  SET @HasOptedOutOfEmail = field(@row,"HasOptedOutOfEmail")
  
  IF @Newsletter == true THEN SET @Newsletterchk = "checked" ENDIF
  IF @Events == true THEN SET @Eventschk = "checked" ENDIF
  IF @Offers == true THEN SET @Offerschk = "checked" ENDIF 
  IF @Hospi == true THEN SET @Hospichk = "checked" ENDIF
  IF @Nrlw == true THEN SET @Nrlwchk = "checked" ENDIF 
  
ELSEIF RequestParameter("submitted") == true THEN

  /* update contact */
  SET @updateRecord = UpdateSingleSalesforceObject(
      "Contact", @contactId,
      "FirstName", RequestParameter("firstname"),
      "LastName", RequestParameter("lastname"),
      "Email", RequestParameter("email"),
   "Phone", RequestParameter("Phone"),
   "Birthdate", RequestParameter("Birthdate"),
      "Preferences_Latestupdates__c", Iif(RequestParameter("newsletter") == "on", "true", "false"),
      "Preferences_Merchandise__c", Iif(RequestParameter("events") == "on", "true", "false"),
      "Preferences_Community__c", Iif(RequestParameter("offers") == "on", "true", "false")
      "Preferences_CorporateHospitality__c", Iif(RequestParameter("hospi") == "on", "true", "false")
      "Preferences_NRLW__c", Iif(RequestParameter("nrlw") == "on", "true", "false")
     )
   
   /* update email in All Subscribers list */  
   IF RequestParameter("email") != AttributeValue("emailaddr") THEN
   SET @email = RequestParameter("email")
   SET @Subscriber = CreateObject("Subscriber")
   SetObjectProperty(@Subscriber, "SubscriberKey", @contactId)
   SetObjectProperty(@Subscriber, "EmailAddress", @email)
   SET @Status = InvokeUpdate(@Subscriber, @createErrDesc, @createErrNo, @createOpts) 
   ENDIF
   
ENDIF

IF @HasOptedOutOfEmail == true THEN
]%%

2 Answers 2

0

Based on the error, it seems the subscriber key hasn't been populated correctly in the retrieve function.

Since you mentioned that you are trying to build a profile center in marketing cloud so it supposes to be a custom profile center.

In this case, you need to populate the subscriber key from the email sending as a parameter:

<a href="%%=RedirectTo(CloudPagesURL(Your_CloudPage_ID, 'SubKey', _subscriberkey))=%%">Update your subscription center here.</a>

From your cloudpage, you need to retrieve value of that parameter after that.

%%[
SET @contactId = RequestParameter('Subkey')

/* the rest of your code here */
......
]%%

References:

0

500 internal server error can mean a lot of things.

One thing I see in your ampscript code is that you haven't closed the last IF statement:

IF @HasOptedOutOfEmail == true THEN

I don't know if you have cut off the page early or if it ends here. An open IF statement would cause a 500 internal server error.

2
  • I think this is closed further down. As it is just above ]%%, I am assuming that below non-ampscript part is only shown to people who indeed have opted out of email, and the ENDIF is set after another ampscript block opener: %%[ Commented Jan 17 at 9:26
  • that's correct, this is the main ampscript , I closed off in the html end Commented Jan 17 at 22:29

You must log in to answer this question.

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