How to set the current category value to a variable which I created.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
Hi,
You have created a variable to store/show the category ?
if that is reference field which is referencing to the categories , you can have advanced reference qualifier to pull the data dynamically on the item you have opened .
if that is a choice field you have to write some script to match the value of your choice field to value of category on the category table.
if you have created a string field - have some script to fetch the data from catalog item.
if this is a single variable not part of any variable set , only available on this item just keep the default value as mobile [value of category on sc_categoty] table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi @ramasatyasa ,
you can try this please follow below steps -
1. Create a Variable
- Go to the Catalog Item (e.g., iPhone).
- Add a Single Line Text variable named category_name.
2. Add a Catalog Client Script
- Navigate to the Catalog Item → Related Links → Catalog Client Scripts
- Click New and configure as follows:
Name: Set Category Name
Type: onLoad
Applies to: Catalog Item (iPhone)
---------------------------------------------------------------------------------------
function onLoad() {
// Get the category name from the catalog item
var categoryName = g_form.getReference('sys_category', function(cat) {
if (cat && cat.name) {
g_form.setValue('category_name', cat.name); // Set the variable value
}
});
}
NOTE -
- sys_category is the field on the catalog item that stores the category reference.
- category_name is the name of the variable you created.
- This script runs onLoad, so it populates the value as soon as the form opens.
Please mark as correct and close the thread if this helps you .
Thanks,
Rithika.ch
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
So you can use a variable of type Text and use onLoad catalog client script + GlideAjax to get the category for the current catalog item and then populate it
This will work for all catalog items and populate the category to which that item belongs when form loads
Script Include: It should be client callable
var CatalogItemCategoryAjax = Class.create();
CatalogItemCategoryAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCategory: function() {
var catItemSysId = this.getParameter('sysparm_cat_item_sysid');
if (!catItemSysId)
return '';
var gr = new GlideRecord('sc_cat_item');
if (gr.get(catItemSysId)) {
var catGr = new GlideRecord('sc_category');
if (catGr.get(gr.category)) {
return catGr.title.toString();
}
}
return '';
},
type: 'CatalogItemCategoryAjax'
});
onLoad Catalog Client Script:
-> Applies on Catalog item - True
function onLoad() {
// Replace 'your_text_variable_name' with the name of your text variable
var textVarName = 'your_text_variable_name';
var url = top.location.href;
var catItemSysId = new URLSearchParams(url).get("sys_id")
var ga = new GlideAjax('CatalogItemCategoryAjax');
ga.addParam('sysparm_name', 'getCategory');
ga.addParam('sysparm_cat_item_sysid', catItemSysId);
ga.getXMLAnswer(function(response) {
if (response) {
g_form.setValue(textVarName, response);
g_form.setReadOnly(textVarName, true);
}
});
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago - last edited 3 hours ago
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();
output
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