User-Defined Functions

Important Update to Custom Scripting

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

Legacy Custom Scripting Language Deprecation Plans 

  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.

Our Custom Scripting language has a lot of built-in flexibility, but you might need to define your own functions in a script. In this example, we'll show you how to create three simple functions:

  1. Add Values
  2. Multiply Values
  3. Divide Values

To get started, you'll want to create a page with two textbox questions. We'll collect the value of X and Y here (to keep things simple).

These questions should have a few validation settings to ensure that the data entered will be usable! At the very least, you'll need to make sure it collects whole and positive numbers. In this example, we've also set a min/max limit from 0-100 but that's not essential, it's just for keeping it simple here!

In the script that follows, we'll get the values for X and Y using sgapiGetValue, so you'll need to replace the (2) and (3) in our example script with the question ID's of your two textbox questions. Once you've identified your question IDs, you can paste the following script into a Custom Scripting element and make those quick edits.

You'll see in the %%output portion of the script that we've created three user-defined functions by adding sgapi to the function name (sgapiAddValues, sgapiMultiply and sgapiDivide). These three functions will take the values of X and Y and output those values added together, multiplied or divided by each other.

/** Show user defined function inside of SurveyGizmo Custom Scripting


/** Just remember to add sgapi to the beginning of your function, you can 
 ** create any function you want in custom scripting.
**/

%%x = sgapiGetValue(2);
%%y = sgapiGetValue(3);

%%output .= "Added values:<br />";
%%output .= sgapiAddValues(%%x, %%y);
%%output .= "<br /><br />";

%%output .= "Muliplied values:<br />";
%%output .= sgapiMultply(%%x, %%y);
%%output .= "<br /><br />";

%%output .= "Divided values:<br />";
%%output .= sgapiDivide(%%x, %%y);
%%output .= "<br /><br />";

/**
 ** Adds two values
 ** @param %%x - value of first text box
 ** @param %%y - value of second text box
 ** @return %%math - new value
 **/
function sgapiAddValues(%%x, %%y) {
	%%math = %%x + %%y;
	return %%math;
}

/**
 ** Multiplies two values
 ** @param %%x - value of first text box
 ** @param %%y - value of second text box
 ** @return %%math - new value
 **/
function sgapiMultply(%%x, %%y) {
	%%math = %%x * %%y;
	return %%math;
}


/**
 ** Divides two values as long as the y is not 0
 ** @param %%x - value of first text box
 ** @param %%y - value of second text box
 ** @return %%math - new value
 **/
function sgapiDivide(%%x, %%y) {
	if (%%y > 0) {
		%%math = %%x / %%y;
		return %%math;
	}
	else {
		return false;
	}
}

Now, if we take one of the functions from above, we can deconstruct it to understand how it works. Let's grab the sgapiDivide, since it has a little more to it! It is written to divide the two values, as long as the value of Y is not 0.

We call the function by name and the values that will be used. We're using an if statement to say that if the value of Y is greater than 0, then go ahead and run the math equation where the value of X is divided by Y. If the value of Y is 0 or less, it will return as false.

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 SurveyGizmo 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!