Create a Sales Cloud-integrated lead capture form using AMPscript

SmartCapture forms provide a quick, user-friendly way to create simple forms and use them in Salesforce Marketing Cloud journeys, but they are not very versatile and often can create more problems than they actually solve.

In this tutorial, you will learn how to create a form using HTML and AMPscript and include functions that will let you quickly push the collected data into your instance of Sales/Service Cloud.

Create an HTML form

First, let’s create a very simple HTML form. If you’ve never created forms in HTML, you can start with reading this short overview on w3schools. The form action method attribute specifies how to send form-data. In this case, we are using post, which appends form-data inside the body of the HTTP request (data is not shown is in URL).

The RequestParameter('PAGEURL') function reloads the page when the form is submitted, posting the form parameters back to the same page, which are then retrieved by the RequestParameter() AMPscript functions. The form has a hidden submitted field. If this value is set to true, which it would after submitting the form, it will trigger the evaluation of the AMPscript functions. Here is a simplified diagram to help you understand this concept:

That’s why in the first step we check if the form has already been submitted or not. Let’s add the AMPscript functions to the form now.

Create form logic using AMPscript

Let’s start simple with creating a Sales Cloud Lead using the data collected in the form. The AMPscript needs to be placed at the top, as it should be evaluated upon form submission and prevent the form fields from being displayed after it’s been submitted. In the below script, we use the CreateSalesforceObject function to create a new Lead:

We can extend the logic a bit by checking first if the Lead with the given email address already exists in Sales Cloud, and if yes – updating their data, or if not – creating a new Lead:

Let’s now add the Lead to a Campaign in Sales Cloud. You will need to hardcode the Campaign Id into the script. Note that you cannot add the same Lead to a campaign twice, so if the Lead has already been added to the campaign, you will get an error. Let’s add creating a new Campaign Member to the script:

And now, let’s put the icing on the cake and send a confirmation email to the Lead. You will need to create a Triggered Send beforehand – if you’re not sure how, read my blog post: Send a triggered email using AMPscript. We will hardcode the Triggered Send external key as @ts_extkey, pass the Lead ID and Email Address to a new Subscriber object and invoke creation of a new Triggered Send object:

You now have a fully functional lead capture form integrated with Sales Cloud!

If you’d like to see this in action, fill in this example form that I created to receive a confirmation email:
https://pub.s10.exacttarget.com/bhu3kciqhjk.

7 thoughts on “Create a Sales Cloud-integrated lead capture form using AMPscript

  1. Vinetia

    Hello, I’m having issues connecting this code to an email link from MC. I use the button and link the corresponding landing page. I also changes the email to subkey. Do you have resources on this already or can provide assistance?

    Like

      1. Vinetia

        Thanks for your reply! The issue I am facing is that the “preview and test” generates the subscriber page and launches the cloud page accurately but does not update the lead but instead creates a new lead. On the other side of the coin, I send a real test email to my inbox as an subscriber and I get and 500 error when clicking the button to the cloud page.

        Like

  2. Dharmendra Singh

    Hi Zuzanna,

    First of all, thank you for your awesome blog. I have one question: In the above example, we are using the static id of the campaign to add the campaign member. Is there any way to make the campaign id dynamic? what is the best way to add the campaign member in the campaign?

    /* add lead to a campaign*/
    SET @CampaignMember = CreateSalesforceObject(“CampaignMember”, 3,
    “CampaignId”, “7011t0000009Cl1”,
    “LeadId”, @leadId,
    “Status”, “Sent”
    )

    Thanks,
    Dharmendra Singh

    Like

    1. Hi Dharmendra – form your email I understand that you have a problem with setting checkbox values. Let me quote my other article here to help you out:
      “For each of the checkboxes, I have added the following in-line IF function: Iif(RequestParameter(“newsletter”) == “on”, “true”, “false”). That is because the checkbox passed from the HTML form will have a value of either on or off, while to pass it to Sales/Service Cloud, we need to convert it to a boolean value of true or false.”
      You can access the full article here: https://sfmarketing.cloud/2020/01/14/custom-profile-and-subscription-center-integrated-with-sales-service-cloud/
      Hope this helps!
      Zuzanna

      Like

    2. For the dynamic Campaign part, you would have to create a @CampaignID variable and set the Campaign ID dynamically based on the rules you will have in place. It could be, for example, a list of checkboxes for different Campaigns that the subscriber wants to subscribe to, and then based on what they checked, you could dynamically add them as a Campaign Member to those Campaigns.

      Like

      1. Dharmendra Singh

        Thanks for your quick response Zuzanna. Please can you send me some dummy code /reference links to make the Campaign ID dynamically?

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s