Check if knowledge base has categories

Luke James
Tera Contributor

Hi All, 

 

I have a requirement for a customer on a knowledge article, where when they choose a knowledge base on the article and that knowledge base has categories attached to it, to then make the category field on the knowledge article mandatory. Any ideas on how to do this? 

 

Been stuck on this for a while, and would really appreciate the help. 

 

Kind Regards, 

 

Luke

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

@Luke James  You need to write onChange client script on knowledge article table then use GlideAjax on it to validate server side data. 

 

Here is my sample script, which is working and i tested on my PDI. 

 

Client script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('validate_kb_cate');
    ga.addParam('sysparm_name', 'getKb_cat');
    ga.addParam('sysparm_cat', newValue);
    ga.getXML(updateCampus);

}

function updateCampus(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer == 'true')
        g_form.setMandatory('kb_category', true);
    else
        g_form.setMandatory('kb_category', false);



}

 

Screenshot : 

kb client script.PNG

 

 

Script Include : 

 

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

    getKb_cat: function() {
        var id = this.getParameter('sysparm_cat');
        var kb = new GlideRecord('kb_category');
        kb.addQuery("parent_id=" + id);
        kb.query();
        if (kb.hasNext())
            return 'true';
        else
            return 'false'
    },

    type: 'validate_kb_cate'
});

 

kb script include.PNG

 

Hope it will help you.

 

Let me know if you have any questions for me.

 

Thanks,

Harsh

View solution in original post

1 REPLY 1

Harsh Vardhan
Giga Patron

@Luke James  You need to write onChange client script on knowledge article table then use GlideAjax on it to validate server side data. 

 

Here is my sample script, which is working and i tested on my PDI. 

 

Client script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('validate_kb_cate');
    ga.addParam('sysparm_name', 'getKb_cat');
    ga.addParam('sysparm_cat', newValue);
    ga.getXML(updateCampus);

}

function updateCampus(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer == 'true')
        g_form.setMandatory('kb_category', true);
    else
        g_form.setMandatory('kb_category', false);



}

 

Screenshot : 

kb client script.PNG

 

 

Script Include : 

 

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

    getKb_cat: function() {
        var id = this.getParameter('sysparm_cat');
        var kb = new GlideRecord('kb_category');
        kb.addQuery("parent_id=" + id);
        kb.query();
        if (kb.hasNext())
            return 'true';
        else
            return 'false'
    },

    type: 'validate_kb_cate'
});

 

kb script include.PNG

 

Hope it will help you.

 

Let me know if you have any questions for me.

 

Thanks,

Harsh