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
9 REPLIES 9

@sattar3 

I hope UI type is set to ALL for that onLoad script

💡 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

@Ankur Bawiskar UI type is ALL always, but it is not working.

That is thing i'm unable to figure it out and fix this issue.

 

Thanks.

@sattar3 

what debugging did you do for native?

alert came?

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

@Ankur Bawiskar I used some alerts to print whether it is making mandatory or not.

In the alerts it shows Mandatory false in backend view and Mandatory true in Front end view.

 

Script with alerts:

function onLoad() {
    checkShippingRequired();
}

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

    alert("Requested For Sys ID: " + reqUserId);

    var ga = new GlideAjax('sn_hamp.ScriptIncludeName');
    ga.addParam('sysparm_name', 'checkShippingRequired');
    ga.addParam('sysparm_user_id', reqUserId);
    ga.getXMLAnswer(analyzeResponse);
}

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

    alert("Script Include Response: " + response);

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

function setShippingFields(required) {

    alert("Shipping Required = " + 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);
        }
    }

    // Single Alert showing final status of all fields
    var msg = "Field Validation Results\n\n";

    for (var j = 0; j < allFields.length; j++) {
        msg += allFields[j] +
            "\nVisible : " + g_form.isVisible(allFields[j]) +
            "\nMandatory : " + g_form.isMandatory(allFields[j]) +
            "\n-------------------\n";
    }

    alert(msg);
}

 

sattar3_0-1786448067367.png

sattar3_1-1786448561873.png

please let me know where it is wrong:

 

Thanks,

Sattar

 

Hi @sattar3,

 

Begin the script as setting every possibility of mandatory variable to be false in this context, then set Display false, then do your checks. 

 

Please Accept this response as Solution if it assisted you with your question & Mark this response as Helpful.

 

Kind Regards,

Ehab Pilloor