How to Prefill a Form With Salesforce Data
Prefill allows us to load or populate data into the form based on certain parameters. In this tutorial we will learn three different examples of using the prefill functionality with your Salesforce forms:
- Static prefill
Prefill a contact record based on a URL attribute
- Dynamic prefill
Prefill a contact record based on user selection
- Dynamic related prefill
Prefill a contact record and its associated account record based on user selection
Create a new form
On your FormCrafts dashboard click on New Form. This will open a popup dialog where you can choose to start from a scratch, select a template, or duplicate an existing form. For this tutorial we would select Blank Form. On the next screen enter the form name and press Enter.
We now have to add fields to our form. Using the Add Field button on the top-right corner of the screen add the following fields to your form:
- A one line input field. Label this one What is your first name?
- Another one line input field. Label this one What is your last name?
- An email field. Label this one What is your email?
- A hidden field. Label this one Contact Email
Next go to Settings -> Others and check the option Prefill fields from URL attributes.
Connect to Salesforce
Click on Salesforce on the top-left corner and then click on Connect. This will open a popup window where you can authorize FormCrafts. We are now ready to configure prefill, which can be done in two ways:
In this case we would fetch a Contact record from Salesforce, based on a fixed parameter in the URL or a hidden field and then display it on our form when it first loads.
Let's say that we want to fetch a contact record that matches a particular email.
Click on Salesforce on the top-left corner and head to the Prefill tab. Click on Add Lookup. Click (select object) and pick Contact. We will notice two tables here.
- Lookup conditions
This is where we specify the conditions to find the record in Salesforce.
- Field mapping
Once we find the record, how do we populate the record details into the form? This is where we configure that.
For this example we have one simply lookup condition: we need the email to match. Click on (Salesforce field) and select Email. Click on (operator) and select is. Click on (search or type) and select the Contact Email field.
The Lookup conditions table will fetch the record. We have two ways to populate the record information into our form: field mapping and form variables.
This method allows us to populate record fields into specific form fields.
Under field mapping click on (Salesforce field) and select First Name. Click on (form field) and select What is your first name?. Click on Add. We will map other form fields in the same way. Our configuration should look like this:
We are telling the integration to fetch the Salesforce record where the email matches the value of our hidden field labelled Contact Email, and then populate that into the form based on field mappings.
Note: the hidden field has no value at the moment. We will pass a value using the URL.
Our configuration is in place. Let us test it. Click on Share / Embed and copy the Easy Link. Now add this string to the end of the link:
Our link should look like this:
Now when we visit this link the form would find a contact with the email firstname.lastname@example.org and fill in the record details into the form.
You can use form variables to show record information in other places like the Custom text field, email content, thank you message, etc ... Let's say we want to display the Contact name and Contact email as text on our form.
Add a Custom text field to your form. Edit the contents of this field and look for Variables. When you click on Variables you would notice that the field properties of our record are available to be used via form variable tags, like [Lookup Contact 3: Name]
In this case we would fetch a Contact record from Salesforce based on a dropdown field in our form that lists some contacts by their email. The prefill data is updated when the user selects a different option for this dropdown field.
This method allows us to use prefill the form with conditions that can change while the user fills out the form.
Remove the Contact Email hidden field from your form, and also remove the corresponding Lookup Conditions item from the Salesforce sidebar.
Now add a dropdown field to your form using the Add Field button, and label it Select Contact. Set the options list to:
Important: each option has two parts. The part before == is the value of the option (and is used to make a lookup). The part after == is the option text displayed to the user.
Now click on Salesforce (top-left) head over to the Prefill tab, and add two new Lookup Condition:
We are telling the integration to fetch the Salesforce record where the email is not empty and matches the value of our dropdown field labelled Select Contact, and then populate that into the form based on field mappings.
Now click on Preview and test out your form.
Dynamic related prefill
In this case we would fetch a Contact record from Salesforce based on a field in our form. Once that record is fetched successfully, we will fetch the parent Account record and populate the Account name into the form as well.
Add another one line input field to your form and label it Account name.
Now open the Salesforce sidebar and go to the Prefill tab. Click on Add Lookup. Click on (select object) and pick Account.
Click on (select lookups) and select the Contact lookup from above. This ensures that our Account lookup is dependent on our Contact lookup.
Under Lookup Conditions click on (select) and find Related Record ID. Now click on (Salesforce field) and select the Contact lookup from above. The object reference would be in the format Lookup Contact X.
Now under Field Mapping map Account Name to Account Name.
Note: The Related Record ID works when we are referencing a child record (like Contact here). If you were trying to reference the parent record (example trying to lookup a Contact from the Account) you would have access to a more specific field like Account ID.
Our final field mapping for this lookup is like this:
Here is our form in action:
Complex lookup conditions
When configuring the lookup conditions we can add condition groups, and also change the operator types for conditions. This allows us to construct really powerful lookup rules.
Clicking on an operator allows us to toggle between AND and OR. We can create lookup conditions that check a field value, or some static text.
Example, we want to prefill records where the email is not empty, and matches the Select Contact field in our form. However, we also want to make sure we only fetch contacts whose department is Sales, or their title contains Head.
Here is how the lookup conditions would look like in that case: