CATEGORY SUBCATEGORY DEPENDENCY ON LOAD

Anupriya-
Tera Expert
Hey Community,
I have a case form and there are dependent choice field as below order-
Entity -> Category -> Sub Category

Client scripts are written to show values based on the value selected in parent fields as below -
1st client script - on change of Entity
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
 
    //Type appropriate comment here, and begin script below
    var caseType = new GlideAjax('EntityMatrix');
    caseType.addParam('sysparm_name', 'getCategories');
    caseType.addParam('sysparm_case_type', g_form.getValue('u_case_type'));
    caseType.addParam('sysparm_entity', newValue);
    caseType.getXML(returnCaseType);
 
 
    function returnCaseType(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var categories = answer;
        var category = categories.split(',');
 
        g_form.clearOptions('u_category');
        g_form.addOption('u_category', '', '-- None --');
 
        // Add the new category choices
        for (var i = 0; i < category.length; i++) {
            g_form.addOption('u_category', category[i], category[i]);
        }
    }
}
 
 
2nd client script: onChange of category as below-
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
 
    //Type appropriate comment here, and begin script below
    var caseType = new GlideAjax('EntityMatrix');
    caseType.addParam('sysparm_name', 'getSubcategories');
    caseType.addParam('sysparm_case_type', g_form.getValue('u_case_type'));
    caseType.addParam('sysparm_entity', g_form.getValue('u_entity'));
    caseType.addParam('sysparm_category', newValue);
    caseType.getXML(returnCaseType);
 
    function returnCaseType(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
console.log("subcategories: " + answer);
        var subcategories = answer;
        var subcategory = subcategories.split(',');
 
        g_form.clearOptions('u_sub_category');
        g_form.addOption('u_sub_category', '', '-- None --');
 
        // Add the new category choices
        for (var i = 0; i < subcategory.length; i++) {
            g_form.addOption('u_sub_category', subcategory[i], subcategory[i]);
        }
    }
}
 
now, there is one problem i am facing with above logic, that it applies only when the values are changed, but after the form is loaded, and if we try to click on any of these fields, it shows all values. What change can be done in the above scripts so that even after the form is loaded, it only show values as per this logic only?

We are using a custom table to store these mappings, so cant use dependent option.

Thanks in advance
1 ACCEPTED SOLUTION

This was achieved by writing another onload client script. Thank you @Ankur Bawiskar  for your responses.

View solution in original post

8 REPLIES 8

@Ankur Bawiskar 
Yes I updated this line.
I am testing on existing records itself. If I comment the below lines as you suggested, the Category is becoming empty onLoad - 

 if (isLoading || newValue === '') {
        return;
    }

@Ankur Bawiskar I am trying on existing records only.
Yes I updated below line also -

 1st Client Script -

caseType.addParam('sysparm_entity', g_form.getValue('u_entity'));

The issue still persists.

This was achieved by writing another onload client script. Thank you @Ankur Bawiskar  for your responses.

@Anupriya- 

Glad to know that my response to have onLoad worked.

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