Form Post Process Script

The Post Process Script gets called after the form submission is validated, before the form submission is saved and before the notification or autoresponder emails are sent. It will only get executed if the form is a valid form.

Use this if you need to have custom processing done on the form data after it was submitted and before it gets saved.

You can do the following things with a Post Process Script:

Post Process Scripts are rendered just like templates are and thus have all of the same logic functionality and variable modification capabilities.

You would access the Post Process Script by editing the form and going to the Post Process Script tab.

Form data is accessed and modified using the form object accessible through the form variable.

Below are the different things that you can do with Post Process Scripts.

Accessing form field data

Each form field is an array and contains the following values:

  • description: The description value for the field.
  • downloadUrl: The URL to download the submitted file from. Only available on file upload fields.
  • fileContents: The contents of the upload file. Only available on file upload fields. Typically used if adding a file to a Request Object submission.
  • filename: The name of the uploaded file including the file extension. Only available on file upload fields.
  • isFormField: A boolean value indicating if the field is an actual form field. The following field types would be set to false:
    content, fieldGroup and html.
  • label: The form field label.
  • name: The form field name.
  • type: The field type.
  • value: The value that was submitted with the form.
  • values: The values for the field if multiple values were available. Typically used with multi-select or multi-checkbox fields.

You can access the field data in the form.fields array. For example, to access the value a single field you would do something like:

{{ form.fields.fieldKey.value }}

Setting form field values

You can modify the form field values before they are saved or included in the notification emails. You can also set values for form fields that were not submitted as long as the field is one that exists for the form.

Below is how you would set a new value for a field:

{% do form.setFieldValue('fieldName', 'new field value') %}


Note that the "fieldName" is the CamelCase version of the form field name - the same as the field layout key.

If you have the value of the field stored in a variable you could pass that as the second parameter:

{% do form.setFieldValue('fieldName', variable) %}

Setting form variable values

You can modify form variable values. Doing so would cause the new value to be saved to the variable after the post process script is run.

Below is how you would set a new value for a variable:

{% do form.setVariableValue('variableName', 'new variable value') %}

Note that the "variableName" is the CamelCase version of the form variable key.

If you have the value for the form variable stored in a variable you could pass that as the second parameter.

{% do form.setVariableValue('variableName', variable) %}

Example: Updating a variable value

In this example, there is a variable called "Submission Count" with a key of "submissionCount". It holds the total number of submissions for a form. It was originally created with a value of "0". We want to increment its value by 1. Below is some sample code to do that:

{% do form.setVariableValue('submissionCount', form.submissionCount + 1) %}

Setting admin notification email addresses

You can either modify an existing set of admin notification email addresses or set a new email address (or multiple addresses) to send the admin notification email to.

"To" email addresses are set in the {#to} variable. It can either be an array of multiple email addresses or a string with a single email address.
"BCC" email addresses are set in the {#bcc} variable. It can either be an array of multiple email addresses or a string with a single email address.

Replacing the existing "to" recipient with a new one

{% do form.setTo('email@company') %}

Replacing the existing "bcc" recipient with a new one

{% do form.setBcc('emailbcc@company') %}

Replacing the existing "to" recipient with multiple addresses

You could pass an array of multiple addresses:

{% do form.setTo(['email@company', 'another@company.com']) %}

Or, you could set one address and then add more later:

{% do form.setTo('email@company') %}
{% do form.addTo('another@company') %}
{% do form.addTo('hola@company') %}

Replacing the existing "bcc" recipient with multiple addresses

You could pass an array of multiple addresses:

{% do form.setBcc(['email@company', 'another@company.com']) %}

Or, you could set one address and then add more later:

{% do form.setBcc('email@company') %}
{% do form.setBcc('another@company') %}
{% do form.setBcc('hola@company') %}

Setting the admin notification email subject

You can either modify the existing email subject or set a new one.

Setting a new email subject

{% do form.setSubject('New Subject') %}

Modifying an existing email subject

{% do form.setSubject(form.subject ~ ' More Subject') %}

Disable sending emails

If you have the admin notification emails enabled then you can disable sending the notification for a single form submission.

If you disable sending emails then the autoresponder email is also disable for that form submission, if the form was originally configured to send them.

{% do form.turnOffEmail() %}

Setting the redirect URL

You can either modify the existing redirect URL or set a new one.

Setting a redirect URL

{% do form.setRedirectUrl('/new/url') %}

Modifying an existing redirect URL

{% do form.setRedirectUrl(form.redirectUrl ~ '?tid=3') %}

Sending data to another service

You can use the Request Object to send data to another service. An example scenario could be that you need to send form data to a CRM. Below is an example of how this could be done.

In this example the form has three fields:

  • First Name (with a field key of firstName)
  • Last Name (with a field key of lastName)
  • Email (with a field key of email)

{% set request = _core.request.getObject('http://www.mycrm.com/api') %}
{% do request.setParam('first_name', form.fields.firstName.value) %}
{% do request.setParam('last_name', form.fields.lastName.value) %}
{% do request.setParam('email', form.fields.email.value) %}
{% do request.post() %}

Marking a form submission as spam

If you have your own method of testing to see if a submission is spam, like a custom honeypot field, then you can flag a form submission as spam. You can also use the Request Object to make an API call to a spam detection API and then mark the submission as spam if appropriate.

For example, you could use OOPSpam or Akismet in the Post Process Script by using the Request Object to make an API call. Based on the response you could mark the submission as spam.

When a submission is marked as spam it still gets saved, but no emails are sent out. When you view the form submission in the administration it shows prominently that it is spam.

Mark a submission as spam

{% do form.setIsSpam() %}

Mark a submission as spam and set a reason

{% do form.setIsSpam('Failed the honeypot test') %}

or

{% do form.setIsSpam('Failed the OOPSpam API test') %}

Debugging

If you're having trouble you can debug variables like you normally would.

Example:

{{ form|debug('Form') }}

If there is any output from the process script then a special page will be displayed that only shows that output. The submission also will not be saved and no emails sent if there is any output from the process script.

Examples

Below are some different examples of how the form process script can be used.

Send specific customers to a different landing page

{% if form.fields.areYouAnExistingCustomer.value == "Yes" %}
  {% do form.setRedirectUrl('/contact-us-thank-you-current-customer') %}
{% endif %}

{% if form.fields.submissionLocation.value == "MD Special Offer" %}
  {% do form.setRedirectUrl('/landing-pages/md-special-offer/thank-you') %}
{% elseif form.fields.submissionLocation.value == "VA Special Offer" %}
  {% do form.setRedirectUrl('/landing-pages/va-special-offer/thank-you') %}
{% endif %}

Send a POST request to an external API to add customer data to a CRM

{% set request = _core.request.getObject('http://www.mycrm.com/api') %}
{% do request.setParam('first_name', form.fields.firstName.value) %}
{% do request.setParam('last_name', form.fields.lastName.value) %}
{% do request.setParam('email', form.fields.email.value) %}
{% do request.post() %}

Send XML data to an external API

{% set xml = '<?xml version="1.0" encoding="utf-8" ?>' %}
{% set xml = xml ~ '<LeadImportDatagram>' %}
{% set xml = xml ~ '<CompanyKey>123456</CompanyKey>' %}
{% set xml = xml ~ '<LeadInfo>' %}
{% set xml = xml ~ '<Contact>{{ form.fields.firstName.value }} {{ form.fields.lastName.value }}</Contact>' %}
{% set xml = xml ~ '<Address>{{ form.fields.address.value }} {{ form.fields.address2.value }}</Address>' %}
{% set xml = xml ~ '<City>{{ form.fields.city.value }}</City>' %}
{% set xml = xml ~ '<State>{{ form.fields.state.value }}</State>' %}
{% set xml = xml ~ '<Zip>{{ form.fields.zipCode.value }}</Zip>' %}
{% set xml = xml ~ '<Phone>{{ form.fields.phone.value }}</Phone>' %}
{% set xml = xml ~ '<Email>{{ form.fields.email.value }}</Email>' %}
{% set xml = xml ~ '<Comment>{{ form.fields.additionalComments.value }}</Comment>' %}
{% set xml = xml ~ '</LeadInfo>' %}
{% set xml = xml ~ '</LeadImportDatagram>' %}

{% set request = _core.request.getObject('http://www.mycrm.com/api') %}
{% do request.setBody(xml) %}
{% do request.setAuthentication('myusername', 'mypassword') %}
{% do request.post() %}

Use a 3rd party vendor such as SendInc to send a secure email for form submissions

{% set request = _core.request.getObject() %}
{% set body = '<p>New form submission on ' ~ form.date ~ '</p>' %}
{% for field in form.fields %}
  {% if field.isFormField %}
    {% if field.type != 'file' %}
      {% set body = body ~ '<p><strong>' ~ field.label ~ '</strong><br />' ~ field.value|nl2br ~ '</p>' %}
    {% else %}
      {% if field.value|length > 0 %}
        {% set body = body ~ '<p><strong>' ~ field.label ~ '</strong><br />See the attached file: ' ~ field.filename ~ '</p>' %}
        {% do request.addFile(field.fileContents, field.filename, field.name) %}
      {% else %}
        {% set body = body ~ '<p><strong>' ~ field.label ~ '</strong><br />[File Not Uploaded]</p>' %}
      {% endif %}
    {% endif %}
  {% else %}
    {% if field.type == 'fieldGroup' %}
      {% for childField in field.childFields %}
        {% set body = body ~ '<p><strong>' ~ childField.label ~ '</strong><br />' ~ childField.value|nl2br ~ '</p>' %}
      {% endfor %}
    {% endif %}
  {% endif %}
{% endfor %}

{% do request.addFile('Custom file', 'custom.txt') %}
{% do request.setAuthentication('email@myemail.com', 'MyPassword') %}
{% do request.setUrl('https://rest.sendinc.com/message.json') %}
{% do request.addParam('email', 'email@myemail.com') %}
{% do request.addParam('recipients', 'you@youremail.com') %}
{% do request.addParam('subject', 'My Awesome Email') %}
{% do request.addParam('message', body) %}
{% do request.post() %}

Use the form to send to a member where their email address is stored in the Members app

{% if form.fields.memberId is defined and form.fields.memberId.value is numeric %}
  {% set member = _api.members.profileFilter.attr({'id': id}).limit('1')|first %}
  {% if member.email is defined and member.email|length > 0 %}
    {% do form.setTo(member.email) %}
  {% endif %}
{% endif %}

Change the email address that the form is emailed to based on a form field value

{% if form.fields.subject.value == 'Question' %}
    {% do form.setTo(['support@domain.com', 'bob@domain.com']) %}
{% elseif form.fields.subject.value == 'Sales' %}
    {% do form.setTo('sales@domain.com') %}
{% endif %}

Send the form email to a specific office based on the zip code of a customer

First create a form variable for each group of zip codes and enter the zip codes in a comma separated list.

{% set cincinnati = form.variables.cincinnati|split(',') %}
{% set cleveland = form.variables.cleveland|split(',') %}
{% set columbus = form.variables.columbus|split(',') %}
{% set fortWayne = form.variables.fortWayne|split(',') %}

{% if form.fields.zip.value in cincinnati %}
  {% do form.setSubject(Customer Request - Cincinnati Office') %}
  {% do form.setTo('cincinnati@domain.com') %}
{% elseif form.fields.zip.value in cleveland %}
  {% do form.setSubject(Customer Request - Cleveland Office') %}
  {% do form.setTo('cleveland@domain.com') %}
{% elseif form.fields.zip.value in columbus %}
  {% do form.setSubject(Customer Request - Columbus Office') %}
  {% do form.setTo('columbus@domain.com') %}
{% elseif form.fields.zip.value in fortWayne %}
 { % do form.setSubject(Customer Request - Fort Wayne Office') %}
  {% do form.setTo('fortwayne@domain.com') %}
{% else %}
  {% do form.setSubject('Customer Request - No Zip Associations') %}
{% endif %}