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
Tera Patron
Tera Patron

@Anupriya- 

are you saying when form is saved and the same form loads again it's not working?

update as this

In both the onChange remove these lines so that it runs when form loads

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

Also in 1st Client script use this to get the field value

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

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

Yes Ankur, You understood the issue correctly. I already tried removing these lines & then when the form is loaded previous filled values displays as empty.

@Anupriya- 

the scripts will run for existing record correctly

But not for new records as there are no values inside those fields as it's a new record

Also did you update this line in 1st CS

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

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

@Anupriya- 

Hope you are doing good.

Did my reply answer your question?

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