Explain the Code--> Making Variables Read Only--Catalog Item?

RAHUL Khanna1
Mega Guru

Hi Folks, 

 

Can you explain the below code ...how to write such code ....

 

function onLoad() {
try{
//Get the 'Variables' section
var ve = $('variable_map').up('table');
//Disable all elements within with a class of 'cat_item_option'
ve.select('.cat_item_option', '.slushselectmtm', '.questionsetreference', '.form-control', '.checkbox', '.html').each(function(elmt){
elmt.disabled = true;
});
//Remove any reference or calendar icons
// ve.select('img[src*=reference_list.gifx]','img[src*=btn-icon.gifx]', 'img[src*=small_calendar.gifx]').each(function(img){
// img.hide();
// });
// //Hide list collector icons
// ve.select('img[src*=arrow]').each(function(img){
// img.up('table').hide();
// });
}
catch(e){}
$("variable_map").querySelectorAll("item").forEach(function(item) {

var variable = item.getAttribute("qname");

g_form.setReadOnly("variables."+ variable, true);

});

//g_form.setVariablesReadOnly(true);
}

1 ACCEPTED SOLUTION

Did you check out the article I linked?  This code was written before the Calgary release of ServiceNow.  I don't recommend using it anymore because it's too inflexible and prone to breaking.  I mention in the article that you should use the 'Write roles' field and Catalog UI policies to make variables readonly now.  I'd recommend one of those methods.

https://www.servicenowguru.com/scripting/business-rules-scripting/variables-form-readonly/

Please mark this question as answered if I've answered your original question.

View solution in original post

4 REPLIES 4

Mark Stanger
Giga Sage

This code uses PrototypeJS (which is available in ServiceNow by default) to manipulate the HTML through javascript.  It can be helpful in client scripts when there's no other way to change the look of elements on a page.  Here is some documentation on Prototype that should help you understand a little bit better.

http://api.prototypejs.org/

I've commented the code below to explain what's going on.  I actually wrote this code way back when at SNGuru.  Here's some more information that may be helpful on the topic.

https://www.servicenowguru.com/scripting/business-rules-scripting/variables-form-readonly/

function onLoad() {
try{ 
//Get the 'Variables' section.  The variables on the form are contained in an HTML element with an ID of 'variable_map'. Get that element and then move up the heirarchy until you find the next 'table' tag.
var ve = $('variable_map').up('table');

//Find all of the elements within the parent element we just selected with the following 'class' attributes (cat_item_option,slushselectmtm,etc. This will return an array of elements which we can iterate through and examine using 'each'
ve.select('.cat_item_option', '.slushselectmtm', '.questionsetreference', '.form-control', '.checkbox', '.html').each(function(elmt){

elmt.disabled = true; //Make each of the discovered elements read only
});

//Remove any reference or calendar icons (needed to make these variable types read only
// ve.select('img[src*=reference_list.gifx]','img[src*=btn-icon.gifx]', 'img[src*=small_calendar.gifx]').each(function(img){
// img.hide();
// });
// //Hide list collector icons
// ve.select('img[src*=arrow]').each(function(img){
// img.up('table').hide();
// });
}
catch(e){}
$("variable_map").querySelectorAll("item").forEach(function(item) {

var variable = item.getAttribute("qname");

g_form.setReadOnly("variables."+ variable, true);

});

//g_form.setVariablesReadOnly(true);
}

RAHUL Khanna1
Mega Guru

I have a question here , This CS is on RITM table, and it makes all the Variables in for any RITM created as read obly. 

My requirement is to make a perticular RITM and that too in that perticular variable field as editable (List COllecter). 

I tried with writing on line script on the required Cat Item with onload scrpit (g_form.setReadObly('variable_name'), false). But was not able to make it editible. 

 

how can I chieve this .

 

Thanks 

Did you check out the article I linked?  This code was written before the Calgary release of ServiceNow.  I don't recommend using it anymore because it's too inflexible and prone to breaking.  I mention in the article that you should use the 'Write roles' field and Catalog UI policies to make variables readonly now.  I'd recommend one of those methods.

https://www.servicenowguru.com/scripting/business-rules-scripting/variables-form-readonly/

Please mark this question as answered if I've answered your original question.

anupamg
Tera Guru

Hi Mark

Thanks for this posting. I am having a similar requirement where I want to make selected variables in a List collector type variable to be read only after values are selected from the available list. I am able to make them read only using command as '$('variablename_select_1').disabled = true; but somehow is not able to grey out 'Remove' btn 

anupamg_0-1681415406266.png

Because of this if user click on it, it becomes editable and is able to move values from 'selected' slush bucket to 'available' .

Any help/guidance in this regard is greatly appreciated.

Regards

Anupam