Populate the subcategory depending upon category on service portal.

Vishwas Kulkarn
Tera Contributor

Hi @Ankur Bawiskar 

I am trying to populate the subcategory depending upon category on service portal but its not working. It show no value in subcategory. Will you help me in this?

Thank you.

 

 Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading == 'true' || newValue == '') {
        return;
    }
    var cat = newValue;
    g_form.clearOptions('subcategory');
    g_form.addOption('subcategory', '', '-- None --');

    var ga = new GlideAjax('global.CatSubcat'); 
    ga.addParam('sysparm_name', 'CatSubcat'); 
    ga.addParam('category', cat); 
    ga.getXMLAnswer(getResponse);

    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);
        var jsonObj = JSON.parse(answer);
        g_form.addOption('subcategory', jsonObj.value, jsonObj.label);
    }
}

 

Script include:

var CatSubcat = Class.create();
CatSubcat.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    CatSubcat: function() {
        var category = this.getParameter('category');
        var jsonObj = {};
        
        var gr = new GlideRecord('sys_choice');
        gr.addQuery('name', 'incident');
        gr.addQuery('element', 'subcategory');
        gr.addQuery('inactive', 'false');
        gr.addQuery('dependent_value', category);
        gr.addOrderBy('sequence');
        gr.query();
        while (gr.next()) {

            jsonObj.value = String(gr.value);
            jsonObj.label = String(gr.label);
        }

        return JSON.stringify(jsonObj);
    },

    type: 'CatSubcat'
});

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Maybe this helps:
Dependent Select Box (Choice) Variables on Record Producers (+ Multi-language!)

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark,

This way is working but I want it on onload. I have requirenment to set the category on onload.

is there any other way?

Thank you.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

follow this link and no script would be required

ServiceNow - Dependent Variables on Record Producer in Service Catalog

Regards
Ankur

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

Hi Ankur,

This way is working but I want it on onload. I have requirenment to set the category on onload.

is there any other way?

Please find below screenshot:

find_real_file.png

I am setting the value of category on onload through URL. But it shows all subcategories.

Thank you.