hiding variables on order guide

patricklatella
Mega Sage

Hi Community,

I've seen a couple threads regarding hiding variables for a catalog item when they are included in an order guide.   I'm using a catalog client script and it appears correct to me, but it's not working.

Anyone see a problem with my script or know what might be preventing this from working?   I'm not seeing the alert in the script either, so it leads me to believe the system is not picking up the client script in the order guide.

function onLoad() {

    //Hiding fields on order guide but keeping visibility on request item

var isOrderGuide = g_service_catalog.isOrderGuide();

alert(isOrderGuide);

if(isOrderGuide){

g_form.setMandatory('ReqType',false);

g_form.setMantatory('Affected_Employee',false);

g_form.setDisplay('ReqType',false);

g_form.setDisplay('Affected_Employee',false);

}

   

}

find_real_file.png

1 ACCEPTED SOLUTION

Community Alums
Not applicable

I tried using this in my Jakarta PDI:



function onLoad() {


    //Type appropriate comment here, and begin script below


    var isOG = g_service_catalog.isOrderGuide();


alert(isOG);


}



In the platform UI, I am getting an error stating 'g_service_catalog' is undefined, and in SP the evaluation is returning nothing. I checked here:https://developer.servicenow.com/app.do#!/document/content/app_store_doc_sp_widget_scripts_istanbul_...



https://developer.servicenow.com/app.do#!/document/content/app_store_doc_sp_widget_scripts_istanbul_...



It appears 'g_service_catalog' is only available in SP in Istanbul and above.



The other way you might go about this is using GlideAjax. Pass in the catalog item name or sys_id, check to see it is an order guide, and if so, then hide the fields.



Script include:



getCatItemName : function() {


var cgr = new GlideRecord('sc_cat_item');


var catItemID = this.getParameter('sysparm_cat_item_id');


//gs.log("TM===> Getting name for " + catItemID, "Q: getCatItemName Ajax");


if(cgr.get(catItemID)){


return cgr.name;


}


}



Catalog client script (onLoad, 'Both' UI Type, applies to 'Catalog Item):


function onLoad() {


/*


// Call GlideAjax


*/


var catItemID = g_form.getUniqueValue();


var ga = new GlideAjax('AjaxUtils');


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


ga.addParam('sysparm_cat_item_id', catItemID);


ga.getXML(switchLabel);



/*


// Swap the label


*/



function switchLabel(response) {


answer = response.responseXML.documentElement.getAttribute("answer");


//alert(answer);


if (answer == "Convert rental/RPO unit to owned") {


//alert('yes');


g_form.setLabel('unr_pi_purchase_price', 'Buyout Amount at End Date (excluding all taxes)');


}



}


}



This is an example of something I wrote to switch the label on a field depending on the catalog item loaded. As I was using variable sets across multiple items and didn't want to create a whole new one just to modify a field label. You should be able to do the same thing and return the catalog item name or type, then hide the fields based on that.



Hope that makes sense!



Cheers,



Tim


View solution in original post

23 REPLIES 23

Community Alums
Not applicable

Have you tried using a UI policy instead? Does 'isOrderGuide()' return true in this case?



Tim


patricklatella
Mega Sage

Hi Tim,


I'm not seeing the alert, so not sure if it's returning true.   Is that where I should see that...in a screen alert?



I have tried a couple UI Policies without success...tried "Class is Order Guide" and then set the actions to hide the variables, but that didn't work either.



Regarding this script, it does not appear to be picking up that it's in an order guide.


Community Alums
Not applicable

If you're not seeing the alert, then I would conclude that the evaluation is returning false; as in, it's not detecting the form is an order guide. l am not 100% clear on your requirements, so just want to clarify:



Are these variables part of a variable set, or just variables added to the form? So, you are using these across multiple catalog items? And you want to hide these fields if the catalog item is an order guide?



I have not seen this method before, so I checked on the developer site:



https://developer.servicenow.com/app.do#!/document/content/app_store_doc_sp_widget_scripts_jakarta__...



This may not work in platform UI, only in Service Portal. Have you tried in both?



Cheers,



Tim


Community Alums
Not applicable

I tried using this in my Jakarta PDI:



function onLoad() {


    //Type appropriate comment here, and begin script below


    var isOG = g_service_catalog.isOrderGuide();


alert(isOG);


}



In the platform UI, I am getting an error stating 'g_service_catalog' is undefined, and in SP the evaluation is returning nothing. I checked here:https://developer.servicenow.com/app.do#!/document/content/app_store_doc_sp_widget_scripts_istanbul_...



https://developer.servicenow.com/app.do#!/document/content/app_store_doc_sp_widget_scripts_istanbul_...



It appears 'g_service_catalog' is only available in SP in Istanbul and above.



The other way you might go about this is using GlideAjax. Pass in the catalog item name or sys_id, check to see it is an order guide, and if so, then hide the fields.



Script include:



getCatItemName : function() {


var cgr = new GlideRecord('sc_cat_item');


var catItemID = this.getParameter('sysparm_cat_item_id');


//gs.log("TM===> Getting name for " + catItemID, "Q: getCatItemName Ajax");


if(cgr.get(catItemID)){


return cgr.name;


}


}



Catalog client script (onLoad, 'Both' UI Type, applies to 'Catalog Item):


function onLoad() {


/*


// Call GlideAjax


*/


var catItemID = g_form.getUniqueValue();


var ga = new GlideAjax('AjaxUtils');


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


ga.addParam('sysparm_cat_item_id', catItemID);


ga.getXML(switchLabel);



/*


// Swap the label


*/



function switchLabel(response) {


answer = response.responseXML.documentElement.getAttribute("answer");


//alert(answer);


if (answer == "Convert rental/RPO unit to owned") {


//alert('yes');


g_form.setLabel('unr_pi_purchase_price', 'Buyout Amount at End Date (excluding all taxes)');


}



}


}



This is an example of something I wrote to switch the label on a field depending on the catalog item loaded. As I was using variable sets across multiple items and didn't want to create a whole new one just to modify a field label. You should be able to do the same thing and return the catalog item name or type, then hide the fields based on that.



Hope that makes sense!



Cheers,



Tim