Variable Set fields in Classic Catalog not working(g_form.setDisplay() and g_form.setMandatory())

sattar3
Tera Contributor

Hello Everyone,

 

I am facing an issue with controlling Variable Set visibility and mandatory fields dynamically using Client Scripts in the Classic Catalog backend view.

 

I have a Catalog Item with a Variable Set called "Shipping Details"

Internal Name: shipping_address_reclaim_asset

Variable Names: first_name, last_name, street_address, apt_street_other, city, state_province, zip_postal_code. 

 

 I have a Script Include that checks if the logged-in user's company has "u_shipping_required = true" in a custom policy table (sn_hamp_territory_procurement). • Based on the result, I want to: → SHOW Variable Set + Make fields mandatory (if shipping required = true) → HIDE Variable Set + Remove mandatory (if shipping required = false)

  Script Include is returning correct response (true/false) GlideAjax call is working correctly STEP 6 alert fires confirming response = 'true' Everything works perfectly in ESC Portal g_form.setDisplay() works on regular catalog variables.

 

  g_form.setDisplay('shipping_address_reclaim_asset', false) → Variable Set still visible for ALL users in backend g_form.setMandatory('first_name', true) → Fields not showing mandatory (*) indicator in backend.

 

Script Include:

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

    checkShippingRequired: function() {
        var isShippingRequired = 'false';

        // Get user passed from client script, fallback to logged-in user
        var targetUserSysId = this.getParameter('sysparm_user_id') || gs.getUserID();

        var userCompanyID = '';

        // Get Company from User record
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(targetUserSysId)) {
            userCompanyID = userGR.getValue('company');
        }
        // Query custom policy table by Company
        if (userCompanyID) {
            var policyGR = new GlideRecord('sn_hamp_territory_procurement');
            policyGR.addQuery('u_company', userCompanyID);
            policyGR.query();

            if (policyGR.next()) {
                var boolVal = policyGR.getValue('u_shipping_required');

                if (boolVal == '1' || boolVal == true || boolVal == 'true') {
                    isShippingRequired = 'true';
                }
            }
        }
        return isShippingRequired;
    },
    type: 'ScriptIncludeName'
});
 

Client Scripts: 

 // OnLoad function onLoad()--> UI Type as All

 

function onLoad() {
    // 1. Get the Sys ID of the 'Requested For' user
    var reqUserId = g_form.getValue('requested_for');

    var ga = new GlideAjax('sn_hamp.ScriptIncludeName');
    ga.addParam('sysparm_name', 'checkShippingRequired');
    // 2. Pass the user ID to the Script Include
    ga.addParam('sysparm_user_id', reqUserId);
    ga.getXMLAnswer(analyzeResponse);
}

function analyzeResponse(response) {
    if (response === 'true') {
 
        g_form.setDisplay('shipping_address_reclaim_asset', true);
        g_form.setMandatory('first_name', true);
        g_form.setMandatory('last_name', true);
        g_form.setMandatory('street_address', true);
        g_form.setMandatory('state_province', true);
        g_form.setMandatory('city', true);
        g_form.setMandatory('zip_postal_code', true);
    } else {
        g_form.setMandatory('first_name', false);
        g_form.setMandatory('last_name', false);
        g_form.setMandatory('street_address', false);
        g_form.setMandatory('state_province', false);
        g_form.setMandatory('city', false);
        g_form.setMandatory('zip_postal_code', false);
        g_form.setDisplay('shipping_address_reclaim_asset', false);

    }
}
 
OnChange of Requested For Client Script: UI Type as All
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    // 1. Get the Sys ID of the 'Requested For' user
    var reqUserId = g_form.getValue('requested_for');

    var ga = new GlideAjax('sn_hamp.ScriptIncludeName');
    ga.addParam('sysparm_name', 'checkShippingRequired');
    // 2. Pass the user ID to the Script Include
    ga.addParam('sysparm_user_id', reqUserId);
    ga.getXMLAnswer(analyzeResponse);
}

function analyzeResponse(response) {
    if (response === 'true') {
      g_form.setDisplay('shipping_address_reclaim_asset', true);
    // alert("Line no.22 "+reqUserId);
        g_form.setMandatory('first_name', true);
        g_form.setMandatory('last_name', true);
        g_form.setMandatory('street_address', true);
        g_form.setMandatory('state_province', true);
        g_form.setMandatory('city', true);
        g_form.setMandatory('zip_postal_code', true);
        //alert("Line no.27 "+reqUserId);

    } else {
        // alert("Line no.32 "+reqUserId);
        g_form.setMandatory('first_name', false);
        g_form.setMandatory('last_name', false);
        g_form.setMandatory('street_address', false);
        g_form.setMandatory('state_province', false);
        g_form.setMandatory('zip_postal_code', false);
        g_form.setMandatory('city', false);
        g_form.setDisplay('shipping_address_reclaim_asset', false);
    }
}
 
Front end(Portal): Working fine in portal
sattar3_0-1786441559855.png

 

Backend: Fields are not mandatory and variable set showing for all users

 

sattar3_1-1786441674801.png
 
Variable Set Screenshot:
sattar3_2-1786441928628.png

 

Only failing in backend catalog view (com.glideapp.servicecatalog_cat_item_view.do) • No JavaScript errors in browser console • GlideAjax response is correct (confirmed via alerts) Any help or guidance is greatly appreciated! So that the script should works in backend as well.
 
Please share any possible way to fix this, or any changes needed in the script?
 
 
Thanks,
Sattar
8 REPLIES 8

Ankur Bawiskar
Tera Patron

@sattar3 

don't use variable set name in script, but use individual variables within that variable set

also add some delay using timeout as Ajax might take some time to bring the value from script include

function onLoad() {
    checkShippingRequired();
}

function checkShippingRequired() {
    var reqUserId = g_form.getValue('requested_for');

    var ga = new GlideAjax('sn_hamp.ScriptIncludeName'); // use correct script include name
    ga.addParam('sysparm_name', 'checkShippingRequired');
    ga.addParam('sysparm_user_id', reqUserId);
    ga.getXMLAnswer(analyzeResponse);
}

function analyzeResponse(response) {
    response = (response || '').toString().trim();

    setTimeout(function () {
        setShippingFields(response === 'true');
    }, 300);
}

function setShippingFields(required) {
    var mandatoryFields = [
        'first_name',
        'last_name',
        'street_address',
        'city',
        'state_province',
        'zip_postal_code'
    ];

    var allFields = [
        'first_name',
        'last_name',
        'street_address',
        'apt_street_other',
        'city',
        'state_province',
        'zip_postal_code'
    ];

    for (var i = 0; i < allFields.length; i++) {
        var field = allFields[i];

        if (required) {
            g_form.setDisplay(field, true);

            if (mandatoryFields.indexOf(field) > -1) {
                g_form.setMandatory(field, true);
            }
        } else {
            g_form.setMandatory(field, false);
            g_form.setDisplay(field, false);
        }
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar , thanks for the quick response.

 

I used the same script in onLoad Client script which didn't work in backend.

Could you please edit the script include name in your response as it is confidential.

Backend view URL contains: instance.service-now.com/com.glideapp.servicecatalog_cat_item_view.do

sattar3_0-1786443426375.png

Thanks,

Sattar

@sattar3 

did you try the script I gave?

if those variables are mandatory 1st make them non-mandatory and then hide

any other UI policies are showing those variables within variable set

Remember UI policy runs at last so it might override your logic

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar , yes i tried the same script in Onload Client script.

Which works in Portal but not in backend.

 

No those fields are not mandatory in the dictionary level, by using script only we are making mandatory.

sattar3_0-1786445567792.png

 

Thanks,

Sattar