Script include

Nipan1
Tera Contributor

Hi All, 

 

I have a requirement to populate a set of value in a lookup select field 'Department' which is getting values from Department table based on the lookup select field 'Business Unit' which is getting values from Business Unit table. 

 

My solution:

1. I wrote an onchange client script to get the Business unit value (getting sys id of the value). 

2. Wrote a script include to first get the name of the Business unit from business_unit table (as I'm getting the sysId from client script) and then try to glide Department table to get the set of values associated with business unit selected by the user. 

 

Script Include:

Nipan1_0-1718000795167.png

Client script:

Nipan1_1-1718000929113.png

 

Issue: I'm unable to get the array of values from the department table. Need help to correct the code. 

 

Thanks,

Nipan

12 REPLIES 12

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Nipan1 ,

 

there are lot of fixes in your script, can you please share the text format of scritp instead of image ?


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Hi @Sohail Khilji ,

 

Thanks for the reply!

Below are the code after some edits, please check. 

 

Script Include:

fetchDepartment: function() {

        var BUValue = this.getParameter('sysparm_bu');
        gs.addInfoMessage('nipan1'+ BUValue);
        var departValue = [];
        var business ='';

        if (BUValue != null) {

            //fetching business unit name from sysid
            var gBU = new GlideRecord('business_unit');
            gBU.addQuery('sys_id', BUValue);
            gBU.query();

            if(gBU.next()){
                business = gBU.getValue('name');
                gs.addInfoMessage('business: '+ business);
            }

            //fetching Department from Business application
            var gDept = new GlideRecord('cmn_department');
            gDept.addEncodedQuery('business_unit=', BUValue);
            gDept.query();

            while (gDept.next()) {
                departValue.push(gDept.getValue('name').toString());    
            }
        }

        gs.addInfoMessage('nipan2'+ departValue);
        return departValue;
    },
 
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var bu = g_form.getUniqueValue('business_unit');
    alert(bu);

    var ga = new GlideAjax("ProblemSBUUtils");
    ga.addParam('sysparm_name', 'fetchDepartment');
    ga.addParam('sysparm_bu', g_form.getValue('business_unit'));
    ga.getXMLAnswer(getResponse);

    function getResponse(response) {
        alert('res1'+ response);
        var choiceOption = '';
        alert(choiceOption);

        // if (response != null && response != '') {
        //     choiceOption = response.split(',');
        //  alert(choiceOption);
        // }
        // for (var j = 0; j < choiceOption.length; j++) {
        //     g_form.addOption('department1', choiceOption[j], choiceOption[j]);
        // }

    }

}
 
 
-Nipan

try :

 

 

Script Include :
fetchDepartment: function() {
 
        var BUValue = this.getParameter('sysparm_bu');
        var departValue = [];
        var business ='';
        if (BUValue != '') {
            var gBU = new GlideRecord('business_unit');
            gBU.addQuery('sys_id', BUValue);
            gBU.query();
            if(gBU.next()){
                business = gBU.getValue('name');
                gs.log('business: '+ business);
            }
            var gDept = new GlideRecord('cmn_department');
            gDept.addQuery('business_unit', BUValue);
            gDept.query();
            while (gDept.next()) {
                departValue.push(gDept.getValue('name').toString());    
            }
        }
        return departValue;
    },
 
 

 
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var bu = g_form.getValue('business_unit');
    var ga = new GlideAjax("ProblemSBUUtils");
    ga.addParam('sysparm_name', 'fetchDepartment');
    ga.addParam('sysparm_bu', g_form.getValue('business_unit'));
    ga.getXMLAnswer(getResponse);
 
    function getResponse(response) {
       var answer = response;
gs.addInfoMessage(answer);
    }
}

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect