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

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.

Matteo M
Tera Contributor

Hi @Mallidi Suma ,
First of all thank you for the support!
I have a few questions more:
I should just change the sysparm ID and the strings inside getsetvalue, am i right?
Where can i take the syparm id of the catalog? I tried but it is not working and im sure that it is my fault. 
If i should add a sub sub category, how should i modify the code?

Thank you in advance for your help  

Hi @Matteo M ,

 

Please find the answers below,

1. I should just change the sysparm ID and the strings inside getsetvalue, am i right?

 

A:- No need of changing anything in this line

"var cat_item_sysID = g_form.getParameter('sysparm_id');".  // This line will automatically fetch the sys_id of your catalog item from the URL.

 

2. Where can I take the syparm id of the catalog? 

A:- No change needed.

 

3.  If i should add a sub-sub category, how should I modify the code?

We need to send the sub-sub category of the catalog item to the Script Include, and we need to dot walk to its 

subcategory and category. Let me know if you need help with this code.

 

Please mark my answers as helpful.

 

Thanks & Regards,

Suma.

Still not working. 

I modified this part as follow, due to different name of variable: 

g_form.setValue('categoria_2_livello',result.SubCategry); //use the backend name of your SubCategory Variable
g_form.setValue('categoria_merceologica',result.Categry); //use the backend name of your Category Variable
g_form.setValue('articolo',result.CatItem); //use the backend name of your Catalog Item Variable
 
In addition, i see that your example is applied to a catalog item but i would like to apply it to a variable sets. Is there any difference?

About the second script "cliencallableajax", i wrote it inside system Ui > script includes; is it correct?