how to get the name of catalog in client script onload

Fdeveloper
Kilo Guru

Hi,

i have a requirement if catalog is company i should hide the button oreder now in catalog item, i create a client script onload and in the client script i have :

function onLoad() {
g_form.setValue('no_order_now', true);
} // it work for me the button is hiding but is hidding in all catalog i should hide the button olnly if the name of catalog of this catalog item is comapnay : 

sc_catalogs='980f534eeb2bb340b28e03b3ca9619f3'//comapny : how can i do that any suggestion please
1 ACCEPTED SOLUTION

Try adding an alert() to check g_form.getUniqueValue() is returning a value.

function onLoad() {
    try {
        var catSysID = g_form.getUniqueValue();
		alert(catSysID);
        var ajax = new GlideAjax('ServiceCatalog');
        ajax.addParam('sysparm_name', 'getServiceCatalog');
        ajax.addParam('sysparm_cat_sys_id', catSysID);
        ajax.getXMLAnswer(function(answer) {
            if (answer.length > 0) {
				if (answer == 'comapny') {  // mispelling of 'company' intended?
					g_for.setValue('no_order_now',true);
				}
            }
        });
    } catch (e) {
        alert(e.message);
    }
}

If the alert is showing a value, check Script Include's "Client callable" is checked and the name of the Script Include is "ServiceCatalog". If changing the name, ServiceCatalog in Client Script's GlideAjax('ServiceCatalog') also needs to be changed to the new name.

find_real_file.png

View solution in original post

5 REPLIES 5

Nayan Mahato
Tera Guru

Hi Fdeveloper,

Instead of using a client script please use a UI policy, where you can give a condition based on the catalog item's name. Thanks.

Please mark my answer as helpful if that solves your concern. Thanks in advance!

Regards,

Nayan

 

Hi @Nayan Mahato ,

i will do this solution if i should hide the button in ine catalog item i should hide the button for 100 catalog item with the catalog name is company

Hitoshi Ozawa
Giga Sage
Giga Sage

To get the service catalog name from the form, it'll be necessary to create a Client Script that'll call Script Include.

Example:

Client Script

function onLoad() {
    try {
        var catSysID = g_form.getUniqueValue();  // get the sys_id of the form
		
        var ajax = new GlideAjax('ServiceCatalog');  // 'ServiceCatalog' is the class name of Script Include that I created
        ajax.addParam('sysparm_name', 'getServiceCatalog'); // name of function in the Script Include
        ajax.addParam('sysparm_cat_sys_id', catSysID);  // parameter to pass to the function
        ajax.getXMLAnswer(function(answer) {
            if (answer.length > 0) {
                alert('service catalog name:' + answer);
            }
        });
    } catch (e) {
        alert(e.message);
    }
}

Script Include:

var ServiceCatalog = Class.create();
ServiceCatalog.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getServiceCatalog: function() {
        var catSysID = this.getParameter('sysparm_cat_sys_id');  // get sys_id of the form passed from client script
        var gr = new GlideRecord('sc_cat_item');
        if (grCat.get(gr.sc_catalogs)) { // query the catalog item table
            var grCat = new GlideRecord('sc_catalog');
            if (grCat.get(gr.sc_catalogs)) {  // query the service catalog table
                return grCat.title;  // return the title
            }
        }
        return ;
    },
    type: 'ServiceCatalog'
});

Execution result:

find_real_file.png

I've set the example form's Catalog to "Service Catalog".

find_real_file.png

hi @Hitoshi Ozawa ,

thank you for your reply don't work i get on console

Uncaught TypeError: Cannot read properties of null (reading 'length')

find_real_file.png

find_real_file.png

and i want get the name of catalog i want once the name of catalog is comapny in client script i will do 

g_for.setValue('no_order_now',true);