After a user fills out their Forms for Salesforce, you have the ability to redirect in several ways. Here's how!
Redirect to a Static Webpage
Redirect to a Dynamic Webpage based on a Form Field
Redirect to a Webpage and Pass Form Responses
Redirect to Another Prefilled Form
Redirect to a Static Webpage
A common use case for Forms for Salesforce is to redirect a user filling out a form to a static webpage, such as a ‘Thank You' page when the user submits a form. This can be configured using Form Settings. Follow these steps:
Step 1: Create a new form or open the existing form that should redirect on submission in Edit mode.
Step 2: In Form Settings, set Post-Submission Options to Redirect users to a new page. In the text box that appears, enter the URL of the webpage you want to redirect to.
Note: You cannot add form field values in the URL. Custom formulas will not work in redirect URLs. To add form field values to the URL, refer to the section below "Redirect to a Webpage and Pass Form Responses."
Step 3: Close Form Settings and publish the form.
Redirect to a Dynamic Webpage based on a Form Field
Using Javascript, a user filling out a form can be redirected to another webpage upon submission based on the value of a form field or fields. This can be helpful to tailor the experience to the user. Follow these steps:
Step 1: Create a new form or open the existing form that should redirect on submission in Edit mode.
Step 2: Add the field or fields to the form that will be used to determine the appropriate webpage. The fields can be hidden or read-only and still be used to determine the appropriate redirect.
Step 3: Open Form Settings and verify that Post-Submission Options are set to Display a confirmation popup.
Note: The form must be able to process code after a successful submission because Javascript will be used to perform the redirect. If the form redirects users to a new page in Form Settings, the Javascript added to the form to run post-submission will never be triggered.
Step 4: Use the following Javascript snippet and replace Field_To_Evaluate with the appropriate API Name of the field, value, and other_value with the appropriate values (as needed), and url_1, url_2, and url_3 with the appropriate URLs (as needed).
// Update Field_To_Evaluate to match the field API Name
// of the field to control the redirect
var fieldToEvaluate = document.getElementById('Field_To_Evaluate');
if (fieldToEvaluate) {
// For a checkbox, use fieldToEvaluate.checked
// Add or remove as many else if statements as needed
// Using else will set a default redirect url
if (fieldToEvaluate.value == 'value') {
location.href = 'url_1';
} else if (fieldToEvaluate.value == 'other_value') {
location.href = 'url_2';
} else {
location.href = 'url_3';
}
}
}
Note: As noted in the code, else if { … } is optional and can be removed if only one value is needed or more can be added to support more than two values. Using else { … } sets a default redirect URL.
If the field that should be evaluated is a checkbox, replace fieldToEvaluate.value == ‘value’ with fieldToEvaluate.checked to evaluate if the box is checked. The same snippet adjusted to evaluate if a checkbox is checked looks like this:
// Update Field_To_Evaluate to match the field API Name
// of the field to control the redirect
var fieldToEvaluate = document.getElementById('Field_To_Evaluate');
if (fieldToEvaluate) {
// For a checkbox, use fieldToEvaluate.checked
// Using else will set a default redirect url
if (fieldToEvaluate.checked) {
location.href = 'url_1';
} else {
location.href = 'url_2';
}
}
}
Step 5: In Form Settings, copy the Javascript with the appropriate text replaced into the Javascript Code field.
Step 6: Publish the form.
Redirect to a Webpage and Pass Form Responses
Sometimes there is the need to pass form field values or responses to another webpage or form while redirecting on submission. Using Javascript, form fields can be appended to the end of a URL to pass as URL query parameters. Follow these steps:
Step 1: Create a new form or open the existing form that should redirect on submission in Edit mode.
Step 2: Add the field or fields to the form that will be passed to the webpage or form when redirected on submission. The fields can be hidden or read-only and still be added to the URL.
Step 3: Open Form Settings and verify that Post-Submission Options are set to Display a confirmation popup.
Note: The form must be able to process code after a successful submission because Javascript will be used to perform the redirect. If the form redirects users to a new page in Form Settings, the Javascript added to the form to run post-submission will never be triggered.
Step 4: Use the following Javascript snippet. Replace base_url with the URL the values should be appended to. The fieldsToAppend array should include all the parameters that need to be passed. Key (Parameter) and values (Field_to_Append) should be added, replaced, and/or removed as needed. The key (Parameter) should be replaced with the name that should be given to the parameter when passed in the url. The value (Field_to_Append) should be replaced with the API Name of the field whose value should be passed. Learn more on how to prefill forms in Forms for Salesforce with URL parameters.
Note: The snippet takes into consideration parameters that may already be included as part of the base url and will append the additional parameters appropriately.
function FF_OnAfterSave() {
// Update base_url to the url the values should be appended to
var baseUrl = 'base_url';
// Update Parameter to match the corresponding parameter
// name desired in the URL
// Update Field_to_Append to match the API Name
// of the field whose value should be appended
var fieldsToAppend = {
'Parameter_1': 'Field_to_Append_1',
'Parameter_2': 'Field_to_Append_2'
}
var fieldArray = [];
for(const p in fieldsToAppend) {
if (fieldsToAppend.hasOwnProperty(p)) {
var f = document.getElementById(fieldsToAppend[p]);
if (f) {
fieldArray.push(p + '=' + encodeURIComponent(f.value));
}
}
}
var redirectUrl = baseUrl + (baseUrl.indexOf('?') !== -1 ? '&' : '?') + fieldArray.join('&');
location.href = redirectUrl;
}
Step 5: In Form Settings, copy the Javascript with the appropriate text replaced into the Javascript Code field.
Step 6: Publish the form.
Redirect to Another Prefilled Form
Using a Prefill Form URL Field and Javascript, a user filling out a form can be redirected to another prefilled form upon submission. This is useful when navigating complex data architectures or trying to improve the performance of large forms with a large number of related objects. Follow these steps:
Step 1: Ensure that a Prefill URL field already exists for the form to be used in the redirect. If the field is not configured, set up Dynamic Prefill for the form.
Step 2: Create a new form or open the existing form that should redirect on submission in Edit mode.
Step 3: Add the Prefill URL field from Step 1 to the form. In the field settings, the field can be marked hidden and read-only. In this example, the field Secondary Prefill URL from the Contact object will be used so the API Name will be Contact.Secondary_Prefill_URL__c.
Step 4: Open Form Settings and verify that Post-Submission Options are set to Display a confirmation popup.
Note: The form must be able to process code after a successful submission because Javascript will be used to perform the redirect. If the form redirects users to a new page in Form Settings, the Javascript added to the form to run post-submission will never be triggered.
Step 5: Use the following Javascript snippet and replace Prefill_URL_Field with the appropriate API Name of the Prefill Url field. As per the earlier example, it would be replaced with Contact.Secondary_Prefill_URL__c.
// Update Prefill_URL_Field to match the prefill url field of the second form
var redirectUrl = document.getElementById('Prefill_URL_Field');
// Set redirect if field exists and has value
if (redirectUrl && redirectUrl.value) {
location.href = redirectUrl.value;
}
}
Step 7: Publish the form.
Step 8: In Publish Options, enable Dynamic Prefill and ensure the object with the Prefill URL field is checked for prefill. Learn more on configuring Dynamic Prefill and generate the prefill links.
Note: The Prefill URL field for the first form cannot be the same as the second form unless the desired result is to redirect back to the same form.
Have any further questions? We’re here to help! Please reach out to support@formstack.com for additional assistance with your use case.