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

Shivalika
Mega Sage

Hello @erin-marie 

 

Why are you using client script to begin with ? 

 

Why not UI policy, with just one you can operate both visible and mandatory. And conditions you can add whatever you want. 

 

What is the hurdle here with ui policy ? 

 

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

erin-marie
Tera Contributor

I am probably overthinking it.  I need to take the product from the catalog form. And then do a lookup on a table to see if it is enterprise which is another field on the lifecycle table; can I do all of that through UI policy?

 

form variable:  sc_sw_noncore_trmproduct

table: sn_apm_trm_standards_product_lifecycle

field name: trm_product

second field name: u_enterprise_wide

if enterprise wide, make catalog form fields visible and mandatory

 

Hello @erin-marie 

 

Okeh so you need to check whether it's enterprise or not on a different table. So this product should be a reference field and you can easily dot walk and check whether it's enterprise or not - correct ? 

 

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=0WynLcOw

NeEISQCY

Hello @erin-marie 

 

If what I am understanding is correct. Please use below script for Script include - Make sure to check Client Callable. 

 

var TRMProductHelper = Class.create();

TRMProductHelper.prototype = {

    initialize: function() {},

 

    getEnterpriseStatus: function(trmProduct) {

        if (!trmProduct) {

            return false; // Return false if no product is provided

        }

 

        var gr = new GlideRecord('sn_apm_trm_standards_product_lifecycle');

        gr.addQuery('trm_product', trmProduct);

        gr.query();

 

        if (gr.next()) {

            return gr.u_enterprise_wide == true; // Returns true/false

        }

        return false;

    },

 

    type: 'TRMProductHelper'

};

 

Below in On change Client scripts - Make sure to mark UI TYPe to - All and variable name to - "sc_sw_noncore_trmproduct" 

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

        return;

    }

 

    // Call the Script Include to get the enterprise-wide status

    var ga = new GlideAjax('TRMProductHelper');

    ga.addParam('sysparm_name', 'getEnterpriseStatus');

    ga.addParam('sysparm_trmProduct', newValue);

 

    ga.getAnswer(function(response) {

        if (response == 'true') {

            g_form.setVisible('sc_sw_noncore_trmdepartment', false);

            g_form.setMandatory('sc_sw_noncore_trmdepartment', false);

        } else {

            g_form.setVisible('sc_sw_noncore_trmdepartment', true);

            g_form.setMandatory('sc_sw_noncore_trmdepartment', true);

 

      }

    });

}

 

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=0WynLcOw

NeEISQCY