Autopopulate catalog item name

Matteo M
Tera Contributor

Hi, 
I have created a catalog with nested categories. Inside categories there are different catalog items. 
An example:  Pen & Pencil > Pen > Blue pen
Blue pen is my catalog item and inside the form there are these fields:
Category: Pen & Pencil
Sub category: Pen
Item: Blue Pen

How can i autoppulate these fields by mean of a catalog client applied to a set of variables?
The idea is that it is autopopulate for any catalog item that i select.

1 ACCEPTED SOLUTION

Mallidi Suma
Tera Guru

Hi @Matteo M ,

 

I recreated your example and used an onLoad Client script and a script Include to achieve the above scenario. Please find the below code Snippets:-

 

Client Script:-

MallidiSuma_0-1684174103463.png

Code Snippet:-

function onLoad() {
   //Type appropriate comment here, and begin script below
var cat_item_sysID = g_form.getParameter('sysparm_id'); // Use the sysparm id of the catalog form the URL.
var ga = new GlideAjax('ClientCallableAjax');
    ga.addParam('sysparm_name', 'getCatSubCat');
    ga.addParam('sysparm_catSysId', cat_item_sysID);
    ga.getXML(getResponse);
 
    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var result = JSON.parse(answer);
g_form.setValue('sub_category',result.SubCategry); //use the backend name of your SubCategory Variable
g_form.setValue('category',result.Categry); //use the backend name of your Category Variable
g_form.setValue('catalog_item',result.CatItem); //use the backend name of your Catalog Item Variable
//alert(result.Categry + result.SubCategry + result.CatItem);
}
}
 
Script Include:-

Screenshot 2023-05-15 at 11.39.51 PM.png

 

Code Snippet:-

var ClientCallableAjax = Class.create();
ClientCallableAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    getCatSubCat: function() {
        var cat_sys_id = this.getParameter('sysparm_catSysId');
        var cat_fields = {};
        var catItemRec = new GlideRecord('sc_cat_item');
        catItemRec.addQuery("sys_id", cat_sys_id);
        catItemRec.query();
        if (catItemRec.next()) {
cat_fields.CatItem = catItemRec.sys_id.toString();
            var subCatRec = new GlideRecord('sc_category');
            subCatRec.addQuery('sys_id', catItemRec.category);
            subCatRec.query();
            if (subCatRec.next()) {
                cat_fields.Categry = subCatRec.parent.toString();
                cat_fields.SubCategry = catItemRec.category.toString();
//gs.info("CatFields"+ cat_fields.Categry + cat_fields.SubCategry);
            }
        }
return JSON.stringify(cat_fields);
    },
    type: 'ClientCallableAjax'
});
Result:-
I am able to auto-populate the category and subcategory onLoad of the form along with the Catalog item name.
Screenshot 2023-05-15 at 11.42.20 PM.png
Please mark my answer as helpful if it helps you !!
 
Thanks & Regards,
Suma.

View solution in original post

7 REPLIES 7

Hi @Matteo M ,

 

Create your Script Include as per my screenshot with the same name and function. 

 

If you are using a Variable set then create a client script in the variable set. There is no difference it should work.

 

Yes, the above change you made is correct.

 

Thanks & Regards,

Suma.

Ankur Bawiskar
Tera Patron
Tera Patron

@Matteo M 

based on particular variable you want to auto-populate some other variables?

which variable is that?

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

Hi @Ankur Bawiskar 

From the service portal i have a catalog with nested categories, and inside categories i have different catalog items.

Now the form of each catalog item is set by a variable sets. These variables set requires to fill in Category, sub category, sub sub category, and catalog item name. So, example: 

From catalog i select the category "item", then "pen and pencil", then "pen", then "blue pen".
The form should be:
Castegory: Item
Sub category: Pen and pencil
Sub sub category: Pen
Catalog item: Blue pen

It should work for any kind of catalog item and category that i will open. So it should be one code working for every form.
Regard,
Matteo