Updating Catalog item variable on click in custom table

MysUser
Tera Contributor

Hello, I'd like to set the attribute of Catalog Item variable to ReadOnly by setting "Active" to false on my custom table. It looks like this:

MysUser_0-1713541488586.png

This variable is reference. I tried to use UI Policy on this table with the script:

 

var variableField = g_form.getControl('u_variable'); 

 var ga = new GlideAjax('CatalogItemUtils');
    ga.addParam('sysparm_name', 'updateVariableReadonly');
    ga.addParam('sysparm_variable', variableField);
    ga.getXMLAnswer(function(response) {
        
    });

 

 

And Script Include:

 

updateVariableReadonly: function(active) {
     
        // Update the field properties based on the checkbox status
        var ac= new GlideRecord('item_option_new');
        ac.addQuery("sys_id",this.getParameter('sysparm_variable'));
		ac.query();
		if(ac.next()){
			ac.setattribute('read_only', 'true');
                        //ac.setAttribute.read_only = true;
	                //ac.read_only = true;
                        ac.update();
        }
    },

 

 

It doesn't work 😞

 

How is it possible to achieve it?

2 REPLIES 2

Bert_c1
Kilo Patron

In you UI Policy try:

 

g_form.setReadOnly('<field_name>', true);

 

And change your script include to return true/false.

 

Community Alums
Not applicable

Hi @MysUser ,

Try this script (few changes I did)-

Client Script
var variableSysId = 'your_variable_sys_id'; // Replace 'your_variable_sys_id' with the actual sys_id of your variable
var ga = new GlideAjax('CatalogItemUtils');
ga.addParam('sysparm_name', 'updateVariableReadonly');
ga.addParam('sysparm_variable', variableSysId); // Pass the variable sys_id
ga.getXMLAnswer(function(response) {
// Handle the response if needed
});
Script Include

var CatalogItemUtils = Class.create();
CatalogItemUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
updateVariableReadonly: function() {
var variableSysId = this.getParameter('sysparm_variable');
var ac = new GlideRecord('your_custom_table'); // Replace 'your_custom_table' with the actual table name
if (ac.get(variableSysId)) {
ac.setValue('read_only', true);
ac.update();
}
}
});

Please mark as Accepted Solution if this solves your query .