The CreatorCon Call for Content is officially open! Get started here.

Getting all variables from an SCTASK

Mike Hashemi
Kilo Sage

I have a client script that runs onLoad. The script determines if the task meets certain conditions, then sets the variables to "read only == false" so that HR can make modifications at the start of an employee onboarding.

 

I have manually created an array of variable names, but only because I do not know how to retrieve all variables from sc_task. A snippet of the script:

 

var hrVars = ['employee_name', 'middle_initial', 'employee_second_name', 'employee_preferred_name', 'employee_title', 'manager_name', 'employee_type', 'start_date_effective_date', 'cost_center'];

for (var i = 0; i < hrVars.length; i++) {
    g_form.setReadOnly(hrVars[i], false);
}

How can I automate the detection of sc_task variables from a client script?

 

2 REPLIES 2

aruncr0122
Mega Guru

Hi @Mike Hashemi ,

 

You don’t actually need to hard-code the variable names. On an sc_task form, the variables are pulled in from the parent RITM and rendered in the variable editor. That means you can loop through whatever variables are actually present on the form instead of maintaining your own array.

Here’s a simple example:

function onLoad() {
var fields = g_form.getFieldNames();
for (var i = 0; i < fields.length; i++) {
if (fields[i].indexOf("variables.") == 0) {
g_form.setReadOnly(fields[i], false);
}
}
}

This will go through all fields on the form and unlock only the ones that are variables.
If you only want to target specific variables (like your HR set), you could either filter them by naming convention (e.g. variables.employee_…) or just keep a smaller array of the ones you care about.

When I run that, I get the following error:

Error in response from AJAX call to global.u_EmpOnboardingAjax().getReqFieldsSctask() script include: g_form.getFieldNames is not a function