Getting all variables from an SCTASK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
9 hours ago - last edited 9 hours ago
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
8 hours ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
7 hours ago - last edited 7 hours ago
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
getReqFieldsSctask looks like this:
getReqFieldsSctask: function() {
// Retrieve SCTASK record
var sctaskId = this.getParameter('sysparm_sctask');
var sctaskGr = new GlideRecord('sc_task');
if (sctaskGr.get(sctaskId)) {
// Check that Catalog Item is Employee Onboarding
if (sctaskGr.cat_item == gs.getProperty('sn_customerservice.u_emp_onboarding.cat_item.id')) {
var shortDesc = sctaskGr.short_description.toString();
// HR Initial Review
if (shortDesc.indexOf('HR Initial Review') > -1)
return "HR";
// Fallback, return null
else
return null;
}
// Fallback, return null
else
return null;
} else
return null;
}
Why is g_form.getFieldNames() calling the u_EmpOnboardingAjax script include?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
7 hours ago - last edited 7 hours ago
Hi @Mike Hashemi,
For sc_task variables, you can grab them dynamically via the g_form.prefixHandlers object in your onLoad client script. Here's a updated snippet:
function onLoad() {
// Your condition check here, e.g., if meets criteria...
var arrVars = [];
var handler = g_form.prefixHandlers['variables'];
if (handler && handler.handlerObject && handler.handlerObject['nameMap']) {
var nameMap = handler.handlerObject['nameMap'];
for (var i = 0; i < nameMap.length; i++) {
arrVars.push('variables.' + nameMap[i].prettyName);
}
}
for (var j = 0; j < arrVars.length; j++) {
g_form.setReadOnly(arrVars[j], false);
}
}
This pulls all present variables from the parent RITM without hardcoding. Tested in recent releases—should unlock just the vars you need for HR edits. If you want to filter (e.g., only HR ones), add a check like if (nameMap[i].prettyName.startsWith('employee_')).
If that doesn't work, please try the below sample script:
function onLoad() {
// g_form.getEditableFields() gives you variable fields too
var variableNames = g_form.getEditableFields(); // returns array of variable names
for (var i = 0; i < variableNames.length; i++) {
var vName = variableNames[i];
// optional: filter by HR variables naming pattern
if (vName.indexOf('employee_') === 0 || vName.indexOf('manager_') === 0) {
g_form.setReadOnly(vName, false);
}
}
}
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
59m ago
Hi @Mike Hashemi ,
In a catalog task (sc_task), the variables don’t technically “belong” to the task table – they live on the parent RITM. That’s why you don’t see a direct way to loop through “all variables” on the task.
If you want to avoid hardcoding names, you have a couple of options:
1. Use g_form.getEditableFields() (on Catalog Items / RITM forms)
This gives you a list of all variables currently on the form. Example:
var fields = g_form.getEditableFields();
for (var i = 0; i < fields.length; i++) {
g_form.setReadOnly(fields[i], false);
}