Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Making selected variable available in portal

Hafsa1
Mega Sage

I have added "sp-variable-editor" available on portal. But I want only few variables to be editable and rest should not be visible in "draft" state.

Currently all variables are visible in portal.

1 ACCEPTED SOLUTION

PraveenK1149237
Tera Expert

I assume you have cloned the widget and then doing the modification. 
so you would have to do the changes in the client script like this

(function() {
// Assume 'data.sc_cat_item._fields' contains the list of variables
for (var field in data.sc_cat_item._fields) {
var variable = data.sc_cat_item._fields[field];

// Check the state of the variable, and whether it's editable or should be hidden
if (data.state === 'draft') {
// If variable is not editable in draft state, hide or make it readonly
if (!isEditable(variable)) {
variable.sys_readonly = true; // Make it readonly
variable.visible = false; // Hide it
}
}
// Optionally, apply more conditions based on the variable's type or other states
}

// Helper function to check if a variable is editable
function isEditable(variable) {
// Logic to decide whether the variable is editable or not
// For example, checking its type or custom flags
return variable.type !== 'readOnlyType'; // Replace 'readOnlyType' with your condition
}
})();


and in the server script like this 

function getValues(table, sys_id, task_id, opened_by) {
var qs = new GlideappVariablePoolQuestionSet();
if (table == "sc_cart_item")
qs.setCartID(sys_id);
else if(table == "sc_task") {
qs.setRequestID(sys_id);
qs.setTaskID(task_id);
} else {
qs.setRequestID(sys_id);
}

qs.load();
var values = {};
var questions = qs.getFlatQuestions().toArray();
for (var i = 0; i < questions.length; i++) {
var q = questions[i];
var o = {value: q.getValue(), displayValue: q.getDisplayValue(), type: q.getType()};

// Check if the variable is editable in the draft state
if (data.state === 'draft' && !isEditable(q)) {
o.sys_readonly = true; // Make it read-only
o.visible = false; // Optionally, hide it
}

if (o.type == 21) // List Collector
o.display_value_list = q.getDisplayValues().toArray();
if (o.type == 25 && q.canDecrypt(opened_by, table))
o.decrypted_value = q.decrypt(o.value);

var qKey = q.getName();
if (typeof qKey == 'undefined' || qKey == '')
qKey = "IO:" + q.getId();
values[qKey] = o;
}
return values;
}

// Helper function to check if a variable is editable
function isEditable(variable) {
// Logic to decide if the variable should be editable
return variable.type !== 'readOnlyType'; // Replace with your condition for editable variables
}


Visibility and Read-Only State: In the script above, we check the state (data.state === 'draft') and based on that, we make variables read-only or hide them. The actual fields might also need to be manipulated using sys_readonly and visible properties, depending on the platform's rendering logic.

The function isEditable() is where you can define your custom conditions to decide if a variable is editable or not based on its type, a flag, or its state.

You might want to apply this logic specifically to certain types of variables, e.g., only making certain field types (like text, select lists, etc.) non-editable.


View solution in original post

1 REPLY 1

PraveenK1149237
Tera Expert

I assume you have cloned the widget and then doing the modification. 
so you would have to do the changes in the client script like this

(function() {
// Assume 'data.sc_cat_item._fields' contains the list of variables
for (var field in data.sc_cat_item._fields) {
var variable = data.sc_cat_item._fields[field];

// Check the state of the variable, and whether it's editable or should be hidden
if (data.state === 'draft') {
// If variable is not editable in draft state, hide or make it readonly
if (!isEditable(variable)) {
variable.sys_readonly = true; // Make it readonly
variable.visible = false; // Hide it
}
}
// Optionally, apply more conditions based on the variable's type or other states
}

// Helper function to check if a variable is editable
function isEditable(variable) {
// Logic to decide whether the variable is editable or not
// For example, checking its type or custom flags
return variable.type !== 'readOnlyType'; // Replace 'readOnlyType' with your condition
}
})();


and in the server script like this 

function getValues(table, sys_id, task_id, opened_by) {
var qs = new GlideappVariablePoolQuestionSet();
if (table == "sc_cart_item")
qs.setCartID(sys_id);
else if(table == "sc_task") {
qs.setRequestID(sys_id);
qs.setTaskID(task_id);
} else {
qs.setRequestID(sys_id);
}

qs.load();
var values = {};
var questions = qs.getFlatQuestions().toArray();
for (var i = 0; i < questions.length; i++) {
var q = questions[i];
var o = {value: q.getValue(), displayValue: q.getDisplayValue(), type: q.getType()};

// Check if the variable is editable in the draft state
if (data.state === 'draft' && !isEditable(q)) {
o.sys_readonly = true; // Make it read-only
o.visible = false; // Optionally, hide it
}

if (o.type == 21) // List Collector
o.display_value_list = q.getDisplayValues().toArray();
if (o.type == 25 && q.canDecrypt(opened_by, table))
o.decrypted_value = q.decrypt(o.value);

var qKey = q.getName();
if (typeof qKey == 'undefined' || qKey == '')
qKey = "IO:" + q.getId();
values[qKey] = o;
}
return values;
}

// Helper function to check if a variable is editable
function isEditable(variable) {
// Logic to decide if the variable should be editable
return variable.type !== 'readOnlyType'; // Replace with your condition for editable variables
}


Visibility and Read-Only State: In the script above, we check the state (data.state === 'draft') and based on that, we make variables read-only or hide them. The actual fields might also need to be manipulated using sys_readonly and visible properties, depending on the platform's rendering logic.

The function isEditable() is where you can define your custom conditions to decide if a variable is editable or not based on its type, a flag, or its state.

You might want to apply this logic specifically to certain types of variables, e.g., only making certain field types (like text, select lists, etc.) non-editable.