Getting all variables from an SCTASK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago - last edited a month 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
a month ago
You can leverage the g_form.getFieldNames() method to retrieve all field names present on the form.....By iterating through this list, you can identify fields that correspond to catalog variables, which are typically prefixed with variables.. This approach allows you to dynamically interact with the variables without the need to manually maintain an array of variable names....
var fields = g_form.getFieldNames();
for (var i = 0; i < fields.length; i++) {
if (fields[i].startsWith("variables.")) {
g_form.setReadOnly(fields[i], false);
}
}
This script iterates through all form fields, checks if the field name starts with variables., and sets it to be editable if it does. It's important to note that this method only affects variables that are present on the form. If a variable is not included in the catalog item or record producer, it won't appear on the form and thus won't be accessible via this script....
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
