Sub - department and department

Chirag Pathak
Tera Contributor

Hi Community,

 

I had a requirement, I have department and sub-department on a catalog item which have reference qualifiers on them of type lookup select box which are all present in the cmn_department table and the sub- departments have a parent department so all the sub-departments have a department but all departments do not have a sub- department now the requirement is that if the selected department has a sub- department then only the sub-department variable should be visible and mandatory or if not the sub-department variable should not be visible. Any help?

1 ACCEPTED SOLUTION

Community Alums
Not applicable

@Chirag Pathak  If you decide to change to reference type follow below steps as I there will be few changes to the script & reference qualifier

 

- In department variable. (parent is empty means it is itself the parent)

Sai149_0-1716979386827.png

- In Sub department variable

Ref Qualifier: javascript:'parent='+current.variables.YourDepartmentFieldName

Sai149_1-1716979436692.png

Script Include:

 

hasSubDepartments: function() {
        var subDept = new GlideRecord('cmn_department');
        subDept.addEncodedQuery('parent='+this.getParameter('sysparm_department'));
        subDept.query();
        return subDept.hasNext();
    },

 

On Change Client script on department variable:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {      
		g_form.setMandatory('sub_department', false);
        g_form.setVisible('sub_department', false);
        return;
    }
    var ga = new GlideAjax('YourScriptIncudeName');
    ga.addParam('sysparm_name', 'hasSubDepartments');
    ga.addParam('sysparm_department', newValue);
    ga.getXMLAnswer(function(answer) {
        alert(answer);
        if (answer === "true") {
            
            g_form.setVisible('sub_department', true);
            g_form.setMandatory('sub_department', true);
        } else if (answer === "false") {
            
			g_form.setMandatory('sub_department', false);
            g_form.setVisible('sub_department', false);
            
        }
    });
}

 

Sai149_2-1716979618737.png

 

Tested in PDI. If you encounter any issue let me know

 

I started answering community questions recently. If my answer helped you in any way, please mark it as helpful or correct. It would be a great boost.

 

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Chirag Pathak 

in this case you need to have onChange client script on that department variable and then show/hide the other variable

I will suggest to use Reference variable instead of lookup select box as onChange on lookup select box won't work

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

Community Alums
Not applicable

Hi @Chirag Pathak ,

 

Fulfilling this requirement using lookup select box involves risk in future when name dept name changes etc. Instead use reference type variables.

 

You can achieve the solution using lookup select box also (but not ideal). Below is the solution if you want to continue with lookup select box

 

In Sub department Variable add below

Reference qualifier: javascript:'parent.name='+current.variables.YourDepartmentVariableName

Variable Attributes: ref_qual_elements=YourDepartmentVariableName

Sai149_0-1716970161625.png

Script Include:

hasSubDepartments: function() {
        var subDept = new GlideRecord('cmn_department');
        subDept.addEncodedQuery('parent.name='+this.getParameter('sysparm_department'));
        subDept.query();
        return subDept.hasNext();
    },

Sai149_2-1716970543511.png

 

On Change Client script on Department:

replace script include name & function name

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
		g_form.setVisible('sub_department',false);
        return;
    }

    var ga = new GlideAjax('YourScriptIncludeName');
    ga.addParam('sysparm_name', 'YourScriptIncludeFunctionName');
    ga.addParam('sysparm_department', newValue);
    ga.getXMLAnswer(function(answer) {
		alert(answer);
        if (answer === 'true') {
            
            g_form.setDisplay('sub_department', true);
            g_form.setMandatory('sub_department', true);
        } else {
            
            g_form.setDisplay('sub_department', false);
            g_form.setMandatory('sub_department', false);
        }
    });
}

 

Sai149_1-1716970470593.png

 

Tested in PDI.

 

I started answering community questions recently. If my answer helped you in any way, please mark it as helpful or correct. It would be a great boost.

Hi Sai,

will this work if I change the type to reference instead?

Community Alums
Not applicable

@Chirag Pathak  If you decide to change to reference type follow below steps as I there will be few changes to the script & reference qualifier

 

- In department variable. (parent is empty means it is itself the parent)

Sai149_0-1716979386827.png

- In Sub department variable

Ref Qualifier: javascript:'parent='+current.variables.YourDepartmentFieldName

Sai149_1-1716979436692.png

Script Include:

 

hasSubDepartments: function() {
        var subDept = new GlideRecord('cmn_department');
        subDept.addEncodedQuery('parent='+this.getParameter('sysparm_department'));
        subDept.query();
        return subDept.hasNext();
    },

 

On Change Client script on department variable:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {      
		g_form.setMandatory('sub_department', false);
        g_form.setVisible('sub_department', false);
        return;
    }
    var ga = new GlideAjax('YourScriptIncudeName');
    ga.addParam('sysparm_name', 'hasSubDepartments');
    ga.addParam('sysparm_department', newValue);
    ga.getXMLAnswer(function(answer) {
        alert(answer);
        if (answer === "true") {
            
            g_form.setVisible('sub_department', true);
            g_form.setMandatory('sub_department', true);
        } else if (answer === "false") {
            
			g_form.setMandatory('sub_department', false);
            g_form.setVisible('sub_department', false);
            
        }
    });
}

 

Sai149_2-1716979618737.png

 

Tested in PDI. If you encounter any issue let me know

 

I started answering community questions recently. If my answer helped you in any way, please mark it as helpful or correct. It would be a great boost.