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

patricklatella
Mega Sage

Hi Tim,


and when you say pass the sys_id, do you mean the sys_id of the catalog item itself?   I only want the variables to be hidden when the item is included in an order guide.   So will this Ajax call determine that?


Community Alums
Not applicable

Yes, I would pass the catalog item sys_id, then check the value of 'sys_class_name' on the record:



getCatItemType : function() {


var cgr = new GlideRecord('sc_cat_item');


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


if(cgr.get(catItemID)){


return cgr.sys_class_name;


}


}



function onLoad() {


var catItemID = g_form.getUniqueValue();


var ga = new GlideAjax('AjaxUtils');


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


ga.addParam('sysparm_cat_item_id', catItemID);


ga.getXML(hideFields);



function hideFields(response) {


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


if (answer == "sc_cat_item_guide") {


// Hide your fields


}


}


}



Something like this should work. I have not tested, may need some tweaking.



Hope that helps!



Tim


Hi Tim ,



Even I have the same query and tried the above code but it doesn't work. Please let me know if you have found a solution to this one.



Thanks,


Sreejith


patricklatella
Mega Sage

Hello Sanjiv,


I only want the fields to not be visible on the catalog items when they are part of an order guide.  



will your suggestion still work?



here's my existing script



function onLoad() {


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


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);


}


When it is a catalog item, and not an order guide, will the user fill in these details?



Please mark this response as correct or helpful if it assisted you with your question.