How to set the current category value to a variable which I created.

ramasatyasa
Giga Contributor

For suppose If I open a form of a catalog item IPhone which is in the category Mobile. Now I want to populate the value Mobile in the variable which I created for that catalog item IPhone and when it comes to its type - maybe it is any type. 

 

5 REPLIES 5

pranita-24
Tera Guru

Hi @ramasatyasa 

 

You can populate the variable on form load using a Catalog Client Script (type: onLoad). Since the Catalog Item belongs to a Category (like Mobile), you can fetch that category and set it in your variable. Example:

 


function onLoad() {
// Replace 'u_category_variable' with your variable name
var catVar = g_form.getGlideUIElement('u_category_variable');
if (catVar) {
// Get Catalog Item sys_id from URL
var itemSysId = g_form.getParameter('sys_id');

// Call a GlideAjax / getReference to fetch the category
var gr = new GlideRecord('sc_cat_item');
if (gr.get(itemSysId)) {
var cat = gr.category.getDisplayValue();
g_form.setValue('u_category_variable', cat);
}
}
}


The Catalog Item (like iPhone) is linked to a Category (like Mobile) through the sc_cat_item table.
Using a client script, you can fetch that Category and set it to your custom variable.