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

ramasatyasa
Tera 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. 

 

1 ACCEPTED SOLUTION

TejasSN_LogicX
Tera Contributor

Hi @ramasatyasa,

Using this code, you can populate the catalogue item category. in catalog item variable

 

1) open the catalogue item variable from and in default value paste this code. 

javascript: current.cat_item.category.getDisplayValue();

 

TejasSN_LogicX_0-1759152371951.png

output 

TejasSN_LogicX_1-1759152422055.pngTejasSN_LogicX_2-1759152472699.png

 

 

 

 

If my response helped you, please mark it as the correct answer and close the thread. This way, others in the community can benefit too.

Regards,
TejasSN_LogicX
ServiceNow Developer | HackaNow Finalist | Community Contributor
📧tejas.adhalrao11@gmail.com
🔗LinkedIn

 

View solution in original post

7 REPLIES 7

Could you explain this:
current.cat_item.category.getDisplayValue();

Hi @ramasatyasa,

We have a catalogue item variable record. In that record we have a catalogue item reference field.

so 

 

  • current
    Refers to the current variable record. 

  • current.cat_item
    Points to the catalogue itemthat this request item belongs to.  gets the value of the catalogue item

  • current.cat_item.category
    From the catalogue item record, it fetches the category (another reference field pointing to it). get value of the catalogue item category by dot walking. 

  • .getDisplayValue()
    Instead of returning the raw sys_id, it returns the readable name (label) of that category. It's showing category display value/frontend value (not the sys_id)

 

And we are setting the variable's default value here. using this code

 

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.