Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Variable set is hiding when submitting from Order guide

SS1510
Tera Contributor

I have an order guide, in which 2 catalog item are there.  one is pc purchase and other is new hire. When i submit from order guide then a variable set in pc purchase should be visible on ritm . When i submit pc purchase standalone then that variable set should not visible on ritm. It is working fine in platform view . But on service portal in ritm the variable set is not hiding.

For that i have applied client script on ritm

SS1510_0-1763715706764.png

Does anyone has any idea?

14 REPLIES 14

@SS1510 

share screenshot of your existing client script.

where have you written that.

It should work with the approach I shared above

💡 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

function onLoad() {

    /*in strict mode ("Isolate script" is true) we don't have access to the window object so it will return null for Service Portal/Mobile and desktop
     * instead we use a try/catch block to test if we have access to g_service_catalog which is only available in Service Portal/Mobile
     */

    /*Service Portal/Mobile code
    ____________________________________________________________________________________________________*/
    try {
        //check if we are in an order guide using the g_service_catalog api for Service Portal and Mobile
        var isOrderGuide = g_service_catalog.isOrderGuide();

        if (!isOrderGuide) {
            //call function to hide the fields on the catalog items if this is an order guide.
            setTimeout(hideVariables, 1000); // wait 1 second before hiding

            hideVariables();
        }
   }
    /*Desktop code
    ____________________________________________________________________________________________________*/
    catch (e) {
        /* test that error message relates to g_service_catalog then execute desktop only code.
         */
        if (/g_service_catalog is not defined/.test(e.message)) {
            var item = g_form.getControl("current_item"); //sys_id of the current item
            var guide = g_form.getControl("sysparm_guide"); //sys_id of the order guide

            //check if the both HTML elements were returned.
            if (item == null && guide == null) {
                return; //return as we are not in a order guide
            }

            //test that both HTML elements were returned and whether they are equal to each other.
            if (item != null && guide != null && item.value == guide.value) {
                return; //return as we are on the "Describe Needs" section of the order guide
            }
            hideVariables(); //hide the variables if the above conditions weren't met.
        }
    }

    function hideVariables() {
        //alert("working");
        //variables to be hidden need to be not mandatory before setting display to false.
        g_form.setMandatory('updated_new_hire_variables', false);
        g_form.setMandatory('seattle_office_onboarding', false);

        g_form.setDisplay('updated_new_hire_variables', false);
        g_form.setDisplay('seattle_office_onboarding', false);
    }
}SS1510_0-1763721146019.png

this i have applied , but it is not working when submitting from order guide

@SS1510 

you said your earlier script is working in native -> then keep it same

Create new one for Catalog Item view i.e. portal and script I shared

💡 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar ,

I have tried but did not work

@SS1510 

what did you try?

-> earlier script you kept as it is and ensure it runs only on RITM

-> new script you created as per what I shared and that's not working on portal? did you check by adding alert what came in url?

-> I edited my script, there was a typo in that (top.location.href)

sharing again here

function onLoad() {
    //Type appropriate comment here, and begin script below

    var url = top.location.href;

    if (url.indexOf('sc_cat_item_guide') == -1) {

        g_form.setMandatory('updated_new_hire_variables', false);
        g_form.setMandatory('seattle_office_onboarding', false);

        g_form.setDisplay('updated_new_hire_variables', false);
        g_form.setDisplay('seattle_office_onboarding', 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader