Basic Syntax

Forsta HX Platform - API Scripting Guide

Basic Syntax

⭐ Forsta Standard Methods (Use These)

// Get value - Forsta method
var answer = f('AQ1').get(); // Returns the current value

// Set value - Forsta method
f('AQ1').set('5'); // Sets the value to "5"

// Check if specific option selected - Forsta method
// .item('3') accesses option with code "3"
// .toBoolean() returns true if selected, false if not
if (f('BQ2').item('3').toBoolean()) {
    console.log('Option 3 is selected');
}

// Get numeric value - Forsta method
// Converts the value to a number
var numValue = f('CQ1').item('2').toNumber();

// Loop through all options - Forsta method
var codes = f('DQ1').domainValues(); // Get array of all option codes
codes.forEach(function(code) {
    // Check each option to see if it's selected
    if (f('DQ1').item(code).toBoolean()) {
        console.log('Selected: ' + code);
    }
});

// Check if field has value - Forsta method
// .toBoolean() on a field (not item) checks if it has a value
if (f('userid').toBoolean()) {
    var userID = f('userid').toString(); // Convert to string
    console.log('User exists: ' + userID);
}

// Only set if not already set - Forsta method
// ! (not operator) inverts the boolean
if (!f('PatientNumber').toBoolean()) { // If empty/no value
    f('PatientNumber').set(123); // Set it
}

ℹ️ jQuery Methods for DOM Manipulation:

Use jQuery methods (.show(), .hide(), .on()) for DOM manipulation, but always use Forsta API methods for data access.

Show/Hide (jQuery for DOM):

// Use quotes with question IDs
f('AQ1').show();
f('BQ2').hide();

// Conditional show/hide
var value = f('CQ3').get(); // Get value with Forsta method
if (value === '1') {
    f('CQ4').show();
} else {
    f('CQ4').hide();
}

Event Listeners:

// Attach change handler
f('AQ1').on('change', function() {
    var val = f('AQ1').get(); // Use Forsta .get() method
    if (val === '1') {
        f('AQ2').show();
    } else {
        f('AQ2').hide();
    }
});