How to find which field/variable are avaliable on catalog form at Service Portal using client script

Prabhu4
Tera Contributor

Hi All,

I have requirement to update only the variable/fields available on the form at service portal and not RITM/SCTASK.
but I am unable to get that information in catalog client script as g_form.getFieldNames() or g_form.isVisible() methods are not available/deprecated in Xanadu version.

So can you please help me or guide me how I can get the fields names that are available in current form through client Script.


Thank you in advance.

 

12 REPLIES 12

Sujatha V M
Kilo Patron
Kilo Patron

@Prabhu4  Please find the link below for reference, 

 

https://www.servicenow.com/community/itom-forum/how-to-get-all-fields-which-are-available-on-form-on...

 

 

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

Hi @Sujatha V M 
Thanks for the reference, I tried to implement the same but getting "There is a JavaScript error in your browser console" attached the screenshot of the same along with the script. Seems like g_form.elements method is no longer available in Xanadu version.

I also tried getControl but still no luck but t
hank you for input.

@Prabhu4 @Could you please try the below syntax accordingly,

 

function onLoad() {
// Retrieve all variables
var variableNames = g_form.getEditableFields(); // Returns an array of field names (variables).

// Loop through and retrieve all variable names
variableNames.forEach(function(variable) {
var variableValue = g_form.getValue(variable); 

// Get the value

alert("Variable Name: " + variable + ", Value: " + variableValue);
});

}

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

@Sujatha V M I tried the above script, it is working in getting the variable names available in catalog(even if they are hidden at the portal level), the  g_form.getEditableFields() is not available in ServiceNow documentation, so I thought I won't work but it worked. Thanks to you.

Also I tried to use isVisible method as well to filter the variable but still no luck as it is returning true only even if the field is hidden on portal. can you suggest any other method to get only variable available at portal only. Attaching the screenshots FYR of both scenarios 


Field2 is hidden on portal level in example catalog.

 

Client Script 
function onLoad() {

// Retrieve all variables
var variableNames = g_form.getEditableFields(); // Returns an array of field names (variables).

// Loop through and retrieve all variable names
variableNames.forEach(function(variable) {
var variableValue = g_form.getValue(variable);

// Get the value

g_form.addInfoMessage("Variable Name: " + variable + ", Value: " + variableValue);

});

}