Performing Basic Math Using Scripting: Add, Subtract, Multiply, and Divide

Important Update to Custom Scripting

The CustomScript Action now supports the LUA programming language. Visit our NEW Lua Scripting Resources!


  1. New accounts (created after October 29, 2018) will only have the option to use Lua in scripts.
  2. As of October 29, 2018 Custom Scripting Actions will default to Lua as the scripting type in the Custom Scripting Action for accounts created before this date. You will be able to switch to the Legacy Custom Scripting; though we highly encourage using Lua.
  3. In the long term, Legacy Custom Scripting Actions will be switched to read-only. The exact date on this is to be determined; we will send notifications well ahead of time.

Do you need to perform basic math within a response? You can do so using our custom scripting. Because our scripting language is based on PHP any of the operators that are available in PHP are available in our scripting language, and thus, in our surveys! Learn more about PHP arithmetic operators.

This tutorial assumes a basic familiarity with Alchemer and Alchemer Custom Scripting. Check out our Scripting Documentation to get started!

Getting Started: Get Familiar with sgapiGetValue and sgapiSetValue

sgapiGetValue and sgapiSetValue are your friends when performing math in surveys; get familiar with these functions before you jump into doing math.

sgapiGetValue* returns the value (reporting value) of an answered question (or null, if not answered). Chances are, when performing math in a survey, you'll want to pull one or more values from a question to use in your aritmetic. For single answer questions, a single value is returned. If you are looking to use the value from this can get quite a bit trickier. Check out the sgapiGetValue Tutorial to learn more.

*sgapiGetValue can be used to pull data from questions on previous pages.

sgapiSetValue** populates the response value of the given question. Chances are, when performing math in a survey, you'll want to use the result elsewhere in the survey; using sgapiSetValue you can store this data in a hidden value or other question for later use/reporting. The sgapiSetValue portion of the script below can be used to populate a hidden value, textbox, essay, radio button, drop-down menu, slider and image choice question. If you wish to populate other questions this can get quite a bit trickier. Check out the sgapiSetValue Tutorial to learn more.

**sgapiSetValue can be used to set data in questions on any page.

Example

Let's say, for example you want to multiply the number of shirts the customer orders (entered in a Textbox question in a survey) by the price so you can get a total amount due.

Check it out in a example survey!

OR

Download a survey with this script to your account.

In the below script we pull the value of the number of shirts Textbox multiply it by the price and set the value of the Total Amount Due Textbox question.

%%numberofshirts = sgapiGetValue(5); //Get value of number of shirts Textbox
%%shirtprice = 15; //Amount of shirt

%%totalamountdue = (%%numberofshirts * %%shirtprice); //Simply change the operator here to perform some other type of arithmetic
  
sgapiSetValue(6,%%totalamountdue); // ID of Hidden Value or Textbox 

Scripting and Other Out-of-the-Box Customizations

We’re always happy to help you debug any documented script. That said, we do not have the resources to write scripts on demand.

If you have customization ideas that you haven't figured out how to tackle, we're happy to be a sounding board for Alchemer features and functionality ideas that might meet your customization. Beyond this, you might want to consult with someone on our Programming Services Team; these folks might have the scripting chops to help you to achieve what you are looking for!

@plans @pro @ent