Create a SmartCapture form prefilled (prepopulated) with Salesforce data

Smart Capture allows you to easily create forms on your Cloud Pages to collect information about your subscribers and then use that data for future sends and campaigns. While creating a Smart Capture form doesn’t require any coding skills and can be done using only the drag-and-drop editor, adding a simple script to your form can significantly increase the user experience. Forms prepopulated with data can be used for any type of campaign, as long as you already have some data about your subscriber in your database. Subscribers are more likely to register for an event or a raffle when they don’t need to manually fill in all the form fields, and the only thing they have to do is to submit the form.

Create a SmartCapture form

The first thing you will have to do is to create a Smart Capture form on your Cloud Page. Depending on your requirements, you can either create your own Data Extension to collect data submitted by your subscribers or use the system Data Extension called CloudPages_DataExtension.

For the purpose of this tutorial, let’s use the system data extension and create a very simple form with three fields: First Name, Last Name and Email Address.

SmartCapture form on a CloudPage

Add AMPscript to retrieve Salesforce data

In this example, we will be retrieving Sales Cloud data to prepopulate the form using the RetrieveSalesforceObjects function. We will be looking up Contacts in Sales Cloud based on the Subscriber Key. Add the following code in a separate content area, above your Smart Capture form:

%%[
VAR @subscriberkey, @email, @firstname, @lastname
SET @subscriberkey = _subscriberkey
IF NOT EMPTY(@subscriberkey) THEN
SET @rs= RetrieveSalesforceObjects("Contact", "Id, FirstName,LastName,Email", "Id", "=", @subscriberkey)
SET @rowCount = rowcount(@rs)
IF @rowCount > 0 THEN
SET @row = row(@rs,1)
SET @email = field(@row,"Email")
SET @firstname = field(@row,"FirstName")
SET @lastname = field(@row,"LastName")
ENDIF
ELSE
SET @email = emailaddr
SET @firstname = ""
SET @lastname = ""
ENDIF
]%%

If you’re using Campaigns in Sales Cloud, which can admit both Contacts and Leads, you can extend your code with a Substring function, so that it’s able to correctly process both:

%%[
VAR @subscriberkey, @email, @firstname, @lastname
SET @subscriberkey = _subscriberkey
IF NOT EMPTY(@subscriberkey) THEN
IF Substring(@subscriberkey, 1,3) == "00Q" THEN
/* Lead */
SET @rs= RetrieveSalesforceObjects("Lead", "Id, FirstName,LastName,Email", "Id", "=", @subscriberkey)
SET @rowCount = rowcount(@rs)
IF @rowCount > 0 THEN
SET @row = row(@rs,1)
SET @email = field(@row,"Email")
SET @firstname = field(@row,"FirstName")
SET @lastname = field(@row,"LastName")
ENDIF
ELSE
/* Contact */
SET @rs= RetrieveSalesforceObjects("Contact", "Id, FirstName,LastName,Email", "Id", "=", @subscriberkey)
SET @rowCount = rowcount(@rs)
IF @rowCount > 0 THEN
SET @row = row(@rs,1)
SET @email = field(@row,"Email")
SET @firstname = field(@row,"FirstName")
SET @lastname = field(@row,"LastName")
ENDIF
ENDIF
ELSE
SET @email = emailaddr
SET @firstname = ""
SET @lastname = ""
ENDIF
]%%

You can also use data stored in Marketing Cloud to prefill the form – just remember, that you will need to use the LookupRows function instead of RetrieveSalesforceObjects.

Prepopulate the data in the form

In order to add the data retrieved from Salesforce to your form, you will have to modify each of the form input fields separately. Click once on the Smart Capture form, and then once again on the field you wish to modify.

Now edit the field in the HTML view and add an inline display of the adequate field retrieved from Sales Cloud as the default value at the end of the HTML input tag:

<input type="text" name="FirstName"
data-field-type="Text" value="%%=v(@firstname)=%%">

Do this for all the fields that should be prepopulated with data. Publish your CloudPage and test it – the fastest way to test the form is to create an email with a button leading to your CloudPage and previewing the email against a subscriber, who is present in your Salesforce database.

You can also connect this form to a journey in Journey Builder to send a confirmation email to anyone who filled it in, update their Salesforce data or add them to a consecutive campaign.

Read more here about Using a Smart Capture Form as a Journey Builder Entry Event.

17 thoughts on “Create a SmartCapture form prefilled (prepopulated) with Salesforce data

  1. Trailblazer

    Great article!
    Could you please explain how can i retrieve the information customers are filling in the phone field bedore it goes to the CloudPage DE ?
    Cause i need to delete the leading 0 and adding ‘33’.

    Like

    1. Hi Trailblazer and thank you for your comment! This should be possible, but would require advanced development. If you’re up for it, Ivan’s article would be a good start: https://ampscript.xyz/how-tos/how-to-enhance-your-forms-with-ajax-and-ampscript/. If this, however, would be too difficult for you to implement, you could look into manipulating the data using SQL, after it has been upserted into the CloudPages Data Extension. Another possibility, is to create your own form, which would allow you to modify the data to your needs before upserting it into a Data Extension.

      Like

  2. Hi, thank you so much for your article. It has helped me immensely to prepopulate several fields. Unfortunately, I am stuck at the account/company field. Can you please advise how do I code this field? Thank you.

    Like

      1. sjk

        Thanks Zuzanna, I’ve worked it out via ampscript. There are 4 different options to pick from that will populate one field and the field gets updated with the new option upon submission 🙂

        Like

  3. Murali

    Hi Zuzanna,

    Thank you for this article, do you know of any connector/solution that would help us in suggesting the address based on the user initial input on the Address field?

    Say per example I have a field named address in the form – As soon as the user try to fill the address like 415 Mission…………. the field need to get a auto suggestion like below

    “415 Mission Street Third Floor San Francisco, CA 94105 USA ”

    Thanks,
    Murali.

    Like

  4. Ken

    Hi Zuzanna,

    Thanks for detailed explanation. I tested this myself and it works perfectly. However, I am using the contact Id as a subscriber key and would like to pre fill it as a hidden attribute. I have tried several different ways but it does not seem to work for me. How would you recommended I can go about achieving this?

    Thanks,
    Ken

    Like

  5. Malik

    Hi Zuzana,

    Couldn’t find the value in HTML under smart capture. can you please help with this as where can i add the ampscript in the below code.

    Email *

    Thanks,
    Malik

    Like

  6. shantanu agnihotry

    Hi Zuzana,

    I want to populate some boolean fields from the data extension. There are some checkbox fields on the smart capture form that are not getting fetched from DE. Could you please give some advice on this?

    Thanks,
    Shantanu Agnihotry

    Like

Leave a comment