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. 

 

4 REPLIES 4

Naveen Kumar V
Tera Expert

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.

ChallaR
Kilo Guru

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

Ankur Bawiskar
Tera Patron
Tera Patron

@ramasatyasa 

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

AnkurBawiskar_0-1759152065006.png

 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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