0

I am new to writing AMPscript. I'm attempting to place an ales Cloud-integrated lead capture form using AMPscript on Cloud Pages within Marketing Cloud using the content builder. I keep getting the 500 error page. When I tested the script on playcode.io, I got this error:

error: Uncaught TypeError: Cannot set property 'onclick' of null.

%%[
var @Id, @firstName, @lastName, @email, @subscriberRows, @subscriberRow
set @Id = RequestParameter("_Subscriberkey")

IF RequestParameter("submitted") == true THEN

  SET @subscriberRows = RetrieveSalesforceObjects(
   "Lead",
   "Id,Email, FirstName, LastName",
   "Id", "=", RequestParameter("@Id")
   )

  IF RowCount(@subscriberRows) > 0 THEN 
  SET @leadId = Field(Row(@subscriberRows, 1), "Id")

  SET @updateRecord = UpdateSingleSalesforceObject(
      "Lead", @leadId,
      "FirstName", RequestParameter("firstname"),
      "LastName", RequestParameter("lastname"),
      "Company", RequestParameter("company"),
      "Email", RequestParameter("email"),
      "LeadSource", RequestParameter("LeadSource"),
      "Distribution", RequestParameter("Distribution__c")
     )

  ELSE

  SET @leadId = CreateSalesforceObject(
        "Lead", 4,
        "FirstName", RequestParameter("firstname"),
        "LastName", RequestParameter("lastname"),
        "Company", RequestParameter("company"),
        "Email", RequestParameter("email"),
        "LeadSource", RequestParameter("LeadSource"),
        "Distribution__c", RequestParameter("Distribution__c")
       )
  ENDIF   
]%% 

<h2>Thank you for submitting the form.</h2>

%%[ ELSE ]%%

<table style="padding: 20px;"><tr><td>
      <h2>Update your menu:</h2>
      <form action="%%=RequestParameter('PAGEURL')=%%" method="Post">
         <label>First name: </label><input type="text" name="firstname" required=""><br>
         <label>Last name: </label><input type="text" name="lastname" required=""><br>
         <label>Company: </label><input type="text" name="company" required=""><br>
         <label>Email: </label><input type="text" name="email" required=""><br>
         <input name="Subscriber Key" type="hidden" value="%%=v(@id)=%%"><br>
         <input name="LeadSource" type="hidden" value="Tester"><br>
         <input name="Distribution__c" type="hidden" value="INTERNAL"><br>
         <input name="submitted" type="hidden" value="TRUE"><br>
         <input type="submit" value="Submit">
      </form>  
</td></tr></table>

%%[ ENDIF ]%%
2

A few things that I spotted in your script:

  1. Replace RequestParameter("_Subscriberkey") with _SubscriberKey. If you put quotes around it, it will be interpreted as a string. You also don’t need to use the RequestParameter function if you pass the Subscriber Key correctly: using the CloudPagesURL function to redirect subscribers from the email to your CloudPage. Always test the form against an actual subscriber. It would also make sense to check in the script if the SubscriberKey has been passed correctly using if (@Id != "") then ...

  2. You are declaring 4 fields when creating a lead, but actually passing 6 fields. Change the 4 to 6.

  3. You are trying to update a lead using a @leadId variable, but earlier you declared an @Id variable. Change @leadId to @Id.

As suggested by Jason, you can also add the try/catch statement for further debugging. Here's what should work:

<script runat="server">
try {
</script>    
%%[
var @Id, @firstName, @lastName, @email, @subscriberRows, @subscriberRow
set @Id = _Subscriberkey

if (@Id != "") then

IF RequestParameter("submitted") == true THEN

  SET @subscriberRows = RetrieveSalesforceObjects(
   "Lead",
   "Id,Email, FirstName, LastName",
   "Id", "=", RequestParameter("@Id")
   )

  IF RowCount(@subscriberRows) > 0 THEN 
  SET @leadId = Field(Row(@subscriberRows, 1), "Id")

  SET @updateRecord = UpdateSingleSalesforceObject(
      "Lead", @Id,
      "FirstName", RequestParameter("firstname"),
      "LastName", RequestParameter("lastname"),
      "Company", RequestParameter("company"),
      "Email", RequestParameter("email"),
      "LeadSource", RequestParameter("LeadSource"),
      "Distribution", RequestParameter("Distribution__c")
     )

  ELSE

  SET @leadId = CreateSalesforceObject(
        "Lead", 6,
        "FirstName", RequestParameter("firstname"),
        "LastName", RequestParameter("lastname"),
        "Company", RequestParameter("company"),
        "Email", RequestParameter("email"),
        "LeadSource", RequestParameter("LeadSource"),
        "Distribution__c", RequestParameter("Distribution__c")
       )
  ENDIF   
]%% 

<h2>Thank you for submitting the form.</h2>

%%[ ELSE ]%%

<table style="padding: 20px;"><tr><td>
      <h2>Update your menu:</h2>
      <form action="%%=RequestParameter('PAGEURL')=%%" method="Post">
         <label>First name: </label><input type="text" name="firstname" required=""><br>
         <label>Last name: </label><input type="text" name="lastname" required=""><br>
         <label>Company: </label><input type="text" name="company" required=""><br>
         <label>Email: </label><input type="text" name="email" required=""><br>
         <input name="Subscriber Key" type="hidden" value="%%=v(@id)=%%"><br>
         <input name="LeadSource" type="hidden" value="Tester"><br>
         <input name="Distribution__c" type="hidden" value="INTERNAL"><br>
         <input name="submitted" type="hidden" value="TRUE"><br>
         <input type="submit" value="Submit">
      </form>  
</td></tr></table>

%%[ ENDIF 

else]%%

Missing subscriber key.

%%[endif]%%
<script runat="server">
Platform.Load("core", "1.1.1");
} catch(e) {
    Write(Stringify(e));
}
</script>

| improve this answer | |
  • This is awesome. Thanks! – Egypte Jan 24 at 17:03
  • Hello, I've been testing the email to cloud page creation and update via form. It seems my form will not update the lead but only create a new lead in Salesforce. Would you have an idea why this is happening? – Egypte Jan 27 at 16:01
1

You can't validate or test your ampscript outside of the platform since this is a proprietary language for Salesforce Marketing Cloud (unless you build your own interpreter).

When you are testing your scripts within a CloudPage, and you are receiving a 500 error, you should be executing your logic within a try/catch statement in order to output script-specific errors that can help you resolve your error. Here's what that looks like:

<script runat="server">
try {
</script>   

/* ALL AMPSCRIPT GOES HERE */ 

<script runat="server">
Platform.Load("core", "1.1.1");
} catch(e) {
    Write(Stringify(e));
}
</script>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.