Catalog Client Script to make a variable not visible and not mandatory depending on custom column

erin-marie
Tera Contributor

We are using the Product Lifecycle table to track all of our hardware and software for Enterprise Architecture.  I have a catalog item for requesting non-core software installations that asks for product and department.  I need to have the department field not visible or mandatory if the product is enterprise-wide.  Here is what I have right now.

 

Thanks,

Erin

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below

    // get value of variable
    var trmproduct = sc_sw_noncore_trmproduct;

    // get product details
    var gr = new GlideRecord('sn_apm_trm_standards_product_lifecycle');
    gr.addQuery('trm_product', trmproduct);
    gr.query();

    var enterprise = '';
    if (gr.hasNext()) {
        gr.next();
        enterprise = gr.u_enterprise_wide;
        if (enterprise == true) {
            gForm_setVisible('sc_sw_noncore_trmdepartment', false);
            gForm_setMandatory('sc_sw_noncore_trmdepartment', false);

        }

    }

}
9 REPLIES 9

Hello @erin-marie 

 

Please confirm if you checked my answer. Kindly mark my answer as helpful and accept solution if it helped you in anyway, so that it can move from unsolved bucket to solved bucket. 

 

Regards, 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0Wyn

LcOwNeE

It isn't working, so I am still looking at it.

Hello @erin-marie 

 

Can you share scripts or snippets where you are stuck ?

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

 

SanjivMeher
Kilo Patron
Kilo Patron

GlideRecord is not recommended in Client Script.

You should be using GlideAjax in this case. Below you can find few examples

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet/ta-p/2312430

 


Please mark this response as correct or helpful if it assisted you with your question.

Chaitanya ILCR
Kilo Patron

Hi @erin-marie ,
try below

adjust as per your variable names and requirements

 

create a client callable script include

var getFieldValues = Class.create();
getFieldValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getValueOfSingleFieldByID: function() {
        var gr = new GlideRecord(this.getParameter('sysparm_table_name'));
        var uniqueFieldToGet = this.getParameter('sysparm_uniquefield');
        uniqueFieldToGet = uniqueFieldToGet ? uniqueFieldToGet : 'sys_id';
        var uid = this.getParameter('sysparm_uniqueid');
        if (gr.get(uniqueFieldToGet, uid)) {
            return gr.getValue(this.getParameter('sysparm_field'));
        }
        return null;

    },

    type: 'getFieldValues'
});

client script

var ga = new GlideAjax('getFieldValues');
ga.addParam('sysparm_name', 'getValueOfSingleFieldByID')
ga.addParam('sysparm_table_name', 'sn_apm_trm_standards_product_lifecycle');
ga.addParam('sysparm_uniqueid', newValue);
ga.addParam('sysparm_uniquefield', 'trm_product');
ga.addParam('sysparm_field', 'u_enterprise_wide');
ga.getXMLAnswer(function(answer) {
    if (answer && (answer == 'true' || answer == '1')) {
        g_form.setMandatory('sc_sw_noncore_trmdepartment', false);
        g_form.setDisplay('sc_sw_noncore_trmdepartment', false);
    } else {
        g_form.setMandatory('sc_sw_noncore_trmdepartment', tue);
    }
});

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya