Populate 2 list collector variables depending on a choice of a Lookup Select Box type variable

rishabh31
Mega Sage

Dear Team,

I am halfway done with the below requirement but stuck in the other part.

Overview:

There are 2 Tables (u_codes and u_code_entitlements)

Table#1: u_codes, has a column: Code name (u_code_name); Type: String

Table#2: u_code_entitlements, has 4 columns:    (i)- Real Code (u_real_code); Type: Reference from Table#1: u_codes,                                                                                          reflecting 'u_code_name' here

                                                                                  (ii)- Entitlement Name (u_entitlement_name); Type: String

                                                                                  (iii)- Entitlement Active (u_entitlement_active); Type: True/False

                                                                                  (iv)- Group Optional (u_group_optional); Type: True/False

 

There are 3 Variables

Variable#1: Code Require (code_require); Type: Lookup Select Box; Lookup from table: u_codes; Lookup value field: Code name (u_code_name)

Variable#2: Selected Code Associated Entitlements (sca_entitlements); Type: List Collector; List table: u_code_entitlements; Reference qual: u_entitlement_active is TRUE

Variable#3: Additional Entitlements (add_entitlements); List Collector; List table: u_code_entitlements; Reference qual: u_group_optional is TRUE

 

Requirement:

#1- On change/selection of Variable#1- 'code_require' display those Entitlement Name (u_entitlement_name) in variable- 'sca_entitlements' which have Entitlement Active (u_entitlement_active) is TRUE.

 

#2- On change/selection of Variable#1- 'code_require' display those Entitlement Name (u_entitlement_name) in variable- 'add_entitlements' which have Group Optional (u_group_optional) is TRUE.

 

Requirement #1 achieved through Client callable script include as shown below-

*Script Include:

 

 

var itemUtils = Class.create();
itemUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getEntitlements: function() {
        var code = this.getParameter('sysparm_code');
        var result = this.newItem('code');
        var entString = [];
        var entGr = new GlideRecord('u_code_entitlements');
        entGr.addQuery('u_real_code.u_code_name', code.toString());     
        entGr.addQuery('u_entitlement_active', 'true');
        entGr.addEncodedQuery('u_real_codeISNOTEMPTY');
        //entGr.addQuery('u_group_optional', 'true');
        entGr.query();
        while (entGr.next()) {
            entString.push(entGr.sys_id.toString());
        }
        result.setAttribute('code', entString.toString());
    },
    type: 'itemUtils'
});					

 

 

 *Catalog Client Script (onChange of Variable#1: Code Require (code_require)):

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var gtCode = new GlideAjax('itemUtils');
    gtCode.addParam('sysparm_name', 'getEntitlements');
    gtCode.addParam('sysparm_code', newValue.toString());
    gtCode.getXML(getEntitlements);

    function getEntitlements(answer) {
        if (answer) {
            var result = answer.responseXML.getElementsByTagName("code");
            var code = result[0].getAttribute("code");
            g_form.setValue('sca_entitlements', code.toString());
        }
    }
}

 

 

 Now when I am trying to achieve Requirement #2, by creating another function in script include it is not working as expected, In Requirement #2 I want to populate/display those Entitlement Name (u_entitlement_name) in variable- 'add_entitlements' which have Group Optional (u_group_optional) is TRUE.

 

Can anyone please help me with the scripts for Requirement #2 to achieve the desired results?

 

Thank You in advance

 

3 ACCEPTED SOLUTIONS

Joshua Quinn
Kilo Guru

Hello @rishabh31 I hope this finds you well.

 

It looks like you just need to add a second element in your result object and use that to fill in the List variable. The updated Catalog Client Script would look something like:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var gtCode = new GlideAjax('itemUtils');
    gtCode.addParam('sysparm_name', 'getEntitlements');
    gtCode.addParam('sysparm_code', newValue.toString());
    gtCode.getXML(getEntitlements);

    function getEntitlements(answer) {
        if (answer) {
            var result = answer.responseXML.getElementsByTagName("code");
            var code = result[0].getAttribute("code");
            var group_code = result[o].getAttribute("group");
            g_form.setValue('sca_entitlements', code.toString());
            g_form.setValue('add_entitlements', group_code.toString());
        }
    }
}

 

And the updated Script Include function would look something like this:

 

var itemUtils = Class.create();
itemUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getEntitlements: function () {
        var code = this.getParameter('sysparm_code');
        var result = this.newItem('code');
        var entString = [];
        var grpString = [];
        var entGr = new GlideRecord('u_code_entitlements');
        entGr.addQuery('u_real_code.u_code_name', code.toString());
        entGr.addQuery('u_entitlement_active', 'true');
        entGr.addEncodedQuery('u_real_codeISNOTEMPTY');
        //entGr.addQuery('u_group_optional', 'true');
        entGr.query();
        while (entGr.next()) {
            entString.push(entGr.sys_id.toString());
            if (entGr.getValue('u_group_optional') == true) {
                grpString.push(entGr.sys_id.toString());
            }
        }
        result.setAttribute('code', entString.toString());
        result.setAttribute('group_code', grpString.toString());
    },
    type: 'itemUtils'
});	

 

I hope this helps and if so, please mark it helpful.

 

If this solution works for you, please mark my answer as the solution to help others who have the same issue.

 

Thanks,

Josh

View solution in original post

In the AJAX function call add a line before the grpString.push() is called, to log the group entitlement:

 if (entGr.getValue('u_group_optional') == true) {
      gs.info('Found Entitlement: ' + entGr.getDisplayValue());
      grpString.push(entGr.sys_id.toString());
}

This will allow us to test if any group optional entitlements are being found.

In the Client Catalog Script, add a conditional to ensure that the group_code variable has data:

var group_codes = group_code.toString();
if(group_codes != ''){
      g_form.setValue('add_entitlements', group_codes);
}

You could also add some logging within the client catalog script to see what the value of the group_code string is.

View solution in original post

@Joshua Quinn Sir hearty thanks for your provided script and logging advise worked.

Through logs I came to know value is not passing.

So I just removed '.toString()' from the very last line of catalog client script. Then tested it and found its working in one go and now No error ''There is a JavaScript error in your browser console." is displaying.

Your understanding is great. Hearty thanks Sir🙂

So Now the Overall final scripts looks like:

Script Include:

var itemUtils = Class.create();
itemUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getEntitlements: function () {
        var code = this.getParameter('sysparm_code');
        var result = this.newItem('code');
        var entString = [];
        var grpString = [];
        var entGr = new GlideRecord('u_code_entitlements');
        entGr.addQuery('u_real_code.u_code_name', code.toString());
        entGr.addQuery('u_entitlement_active', 'true');
        entGr.addEncodedQuery('u_real_codeISNOTEMPTY');
        //entGr.addQuery('u_group_optional', 'true');
        entGr.query();
        while (entGr.next()) {
            entString.push(entGr.sys_id.toString());
            if (entGr.getValue('u_group_optional') == true) {
                grpString.push(entGr.sys_id.toString());
            }
        }
        result.setAttribute('code', entString.toString());
        result.setAttribute('group_code', grpString.toString());
    },
    type: 'itemUtils'
});	

 

Catalog Client script onchange of Variable#1 (code_require)

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var gtCode = new GlideAjax('itemUtils');
    gtCode.addParam('sysparm_name', 'getEntitlements');
    gtCode.addParam('sysparm_code', newValue.toString());
    gtCode.getXML(getEntitlements);

    function getEntitlements(answer) {
        if (answer) {
            var result = answer.responseXML.getElementsByTagName("code");
            var code = result[0].getAttribute("code");
            var group_code = result[o].getAttribute("group");
            g_form.setValue('sca_entitlements', code.toString());
            g_form.setValue('add_entitlements', group_code); //'.toString()' part is removed
        }
    }
}

 Marking your provided initial script as Correct and accepted as a solution.

View solution in original post

12 REPLIES 12

@Joshua Quinn Sir, there is some unusual behaviour at Variable#3. earlier it was working fine like when I select/change of Variable#1, so the selected Job code (code require) associated entitlement name is displayed in variable- 'add_entitlements' which has Group Optional (u_group_optional) as TRUE.

But now as shown in the screenshot below, even when variable-Job code (code require) is not selected (none) or if selected or if change, the variable #3 (additional entitlements/Does the user require any of these additional access entitlements) is all the time displaying entitlement names which have Group Optional as True, now it is not dependent on change/selection of Variable#1 as desired.

 

rishabh31_0-1683601054486.png

I don't know what went wrong here but it is working like this only, I kept the reference qualifier for Variable#2 and #3 as 'u_entitlement_active: True' and 'u_group_optional: True' respectively as mentioned in my posted question as well.

The script is the same which you provided (.toString() is removed from the last line of the client script).

I checked if there is any other script running for this or any UI policy due to which this unexpected behaviour occurred, but nothing was found that potentially caused this.

NOTE: Variable#2 is working fine on change of Variable#1

 

Requesting your further help on this.

 

Thanks in advance

Hello @rishabh31 ,

 

I hope this finds you well. The issue here is that since no Job Code has been selected in Variable 1, the default reference qualifier is taking over and so you are seeing all Entitlements where Group Optional is set to true.

 

My recommendation would be to use a UI Policy to hide variables 2 and 3 until a value is selected in Variable 1.

 

Hope you found this helpful.

Hi @Joshua_Quinn Sir, Thanks for the reponse

I tried this way too and tested through onchange of variable 1 but did not work as expected because when selecting other variable1 value as well reflect the same variable3

values.

 

 

 

@rishabh31 ,

It sounds like your script include is not returning the values you expect after variable 1 has been selected. Try clearing the values with an onLoad client script. This will remove all selectable choices from the dropdown until a value is selected in variable 1 with group options. To do this, the script should look something like: 

g_form.clearOptions('add_entitlements');

@Joshua_Quinn Sir, tried but no luck, I rechecked the values of variables and all the semicolons, braces, logic but all seems fine. I wrote only 1 line on onload script 

function onLoad() {
g_form.clearOptions('add_entitlements');
}

 

Somewhere by any means, it takes only those values in variable 3 which has Group optional true, it is not depending on changes of variable1 as per our Script Include and Onchange CS.

 

Just discussing can we do something in 'Variable attributes' part for variable#3 (currently kept empty) as it already contains ref qual- group optional as True but no variable attributes.

I mean to say like to call the script include here or something as you seems to be expert on advising the further steps.

 

If yes please help further.

 

Thanks