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

Hi @Joshua Quinn Sir,

I tried the script provided, and onchange of code_require, I 1st requirement is working fine but for second requirement list variable is not populating any variable and throws error 'There is a JavaScript error in your browser console'.

 

Requesting to help further.

 

Thanks

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.

@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.