Back

How To: Custom JavaScript Functions in Calculated Fields

Share

Join our newsletter!

Receive the latest data collection news in your inbox.

In this blog post, we are going to take a look at how we can use custom JavaScript for forms to expand the ability of our calculated fields.

Note: This is a somewhat advanced use of FormAssembly. If you’re not comfortable editing custom code, you might want to build up those skills before attempting this tutorial. Here is some information from our documentation to get you started. Please note that our Success Team does not support custom code help, however we do work with a number of partners who may be able to assist you. Please email us at partners@formassemblycom to learn more.

Example Use Case: Indianapolis Colts Super Fan Application

Let’s examine an hypothetical situation for this how-to blog:

You are the FormAssembly admin for the Indianapolis Colts. Due to recent success, the Colts are experiencing more applications for season tickets than they can possibly accommodate. However, first-come, first-served is boring and outdated, and the Colts want the most passionate and knowledgeable fans filling up the stands on Sundays. In order to do that, they want to put some Colts-related questions on the application page and have FormAssembly calculate the application score.

Form Setup

The application will collect some personal information and answers to Colts-related questions.

You will need to make every field, but the application score, a variable, and name them accordingly – NAME, MOVEDATE, KICKER, QBS. Then, make application score a calculated field.

Custom Functions

In order to calculate the right application score, we need to put together some JavaScript functions that we can use in calculated fields. Every function we’re going to write takes one parameter – the value of a field – and return a numerical value.
The goal of these functions is to assign a certain value to each question and use that to calculate the overall score of the applicant (i.e. How big of a fan they actually are and whether they should be first in line for season tickets).

<script>
const calculateNameScore = (val) => {
       return val.indexOf('Irsay')>-1 || val.indexOf('Polian')>-1 || val.indexOf('Manning')>-1 || val.indexOf('Ballard')>-1 ? 10 : 0;
   }
   const calculateMoveDateScore = (val) => {
       return new Date(val).getTime() === new Date('03/29/1984').getTime()? 10: 0;
   }
   const calculateKickerScore = (val) => {
       return val === 'Adam Vinatieri'? 10: 0;
   }
   const calculateQBsScore = (val) => {
       return val === 'tfa_5tfa_6tfa_700tfa_10'? 20:0;
   }
</script>

Now that we have our custom JavaScript functions in place, we need to add them to our calculated field like so

calculateNameScore(NAME)+calculateMoveDateScore(MOVEDATE)+calculateKickerScore(KICKER)+calculateQBsScore(QBS).

Results

Now when someone fills out this form, their application score is immediately calculated in the form, then sent to the Colts’ FormAssembly admin (you, in this hypothetical situation).

The steps outlined in this Javascript for forms tutorial could be used in a variety of other applications, including grant applications, support requests, exams, certifications, etc.

Like this tutorial? Read a past how-to post on how to back up form submissions in Salesforce notes.

Don’t just collect data
— leverage it