Hide a field in catalogue item which is referenced in a table

Sivakrishna
Kilo Sage

Hi,

     I have a table called frame work rules, in the table two fields is there:

1.hide Catalog item description(This is Check Box type field)

2.Catalogue Item(This is reference field from sc_cat_item)

My requirement is: 

When ever I will make the Check box field i.e. "hide Catalog item description" as true , the catalogue item description should be hided. I have tried the Client Script and Script Include as below but I didn't get the expected result:

 

Please help me.......................

 

onChange Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
var cat = g_form.getValue('catalogue_item');
alert(cat);
var ga = new GlideAjax('hideCatalogDescription');
ga.addParam('sysparm_name', 'getCatalogDescription');
ga.addParam('sysparm_catalogue_item',cat);
ga.getXMLAnswer(function(answer){
var response = JSON.parse(answer);
alert(response);
if(cat=='e03d39bbdbd88f0077ccbc4ffe96193f'){
var ans = g_form.getValue('u_hide_description');
    alert(ans);
    if (ans == 'true') {
alert('Entered if Loop');
 
g_form.setDisplay('response.description', true); // hide if france
}
}
 
}
 
    //Type appropriate comment here, and begin script below
);
}
 
 
Script Include: Client Callable
-----------------
var hideCatalogDescription = Class.create();
hideCatalogDescription.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getCatalogDescription: function(){
var bool = '';
var cate = new GlideRecord('sc_cat_item');
cate.addQuery('sys_id',this.getParameter('sysparm_item'));//sys_id of the Catalogue item
cate.query();
gs.info('Chinni'+' : '+ JSON.stringify(cate));
if(cate.next()){
bool = cat.getValue('description');

}
return JSON.stringify(bool);
},
type: 'hideCatalogDescription'
});

With Regards

 

P.Sivakrishna

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

Assuming you are getting the expected info messages and alerts, if what you want to do is hide the Description field of the Catalog Item based on the response, your setDisplay line needs to be replaced with something more like this:

if (ans == 'true') {
    alert('Entered if Loop');
    document.getElementsByClassName("catalog_description")[0].style.display='none';
} else {
    document.getElementsByClassName("catalog_description")[0].style.display='block';
}

Note that this will not work in Service Portal/ESC, and the Isolate script box must be unchecked as this uses the verboten DOM manipulation.  A supported solution would be to leave the Description field blank on this Catalog Item and populate a label variable at the top of the ordered list onLoad, then hide this variable if your conditions are met.

Aniket Chavan
Tera Sage
Tera Sage

Hello @Sivakrishna ,

I have made some changes in your provided code, so please test it once from your end and let me know if it still not working for you.

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    
    var cat = g_form.getValue('catalogue_item.description');
    alert(cat);

    var ga = new GlideAjax('hideCatalogDescription');
    ga.addParam('sysparm_name', 'getCatalogDescription');
    ga.addParam('sysparm_catalogue_item', cat);
    
    ga.getXMLAnswer(function(answer) {
        var response = JSON.parse(answer);
        alert(response);
        
        if (cat == 'e03d39bbdbd88f0077ccbc4ffe96193f') {
            var ans = g_form.getValue('u_hide_description');
            alert(ans);
            if (ans == 'true') {
                alert('Entered if Loop');
                g_form.setDisplay('catalogue_item.description', false);
            }
        }
    });
}

 

Script Include: 

var hideCatalogDescription = Class.create();
hideCatalogDescription.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getCatalogDescription: function () {
        var bool = '';
        var cate = new GlideRecord('sc_cat_item');
        cate.addQuery('sys_id', this.getParameter('sysparm_catalogue_item'));
        cate.query();

        gs.info('Chinni' + ' : ' + JSON.stringify(cate));

        if (cate.next()) {
            bool = cate.getValue('description');
        }

        return JSON.stringify(bool);
    },
    type: 'hideCatalogDescription'
});

Also later if that works don't forget to remove the sys_id of catalog item from the client script and use the property by using script include.

 

Please mark my answer helpful and correct if I have answered your question.

Thanks & Regards,
Aniket.