Unable to get dependent field for List collector

Community Alums
Not applicable

Hi Community,

 

I have a problem with some list collector field which used to be a reference field previously, the field name is account ID and once we select the Account ID there's a field called Roles that will populate roles from the table related to the account selected it was working file until we changed the reference field to List collector multiple choice, so now when we select , multiple Account ID's the role field is not  showing any value, below is the script

 

 

 

Script include


    getAccountRoles: function() {
        var provider = this.getParameter('sysparm_provider');
        var deptSysId = this.getParameter('sysparm_deptSysId');
        var account_id = this.getParameter('sysparm_acc_id');
        var roleList = [];
        var gr_cloud_roles = new GlideRecord("u_cloud_roles");
        var eQuery = "";
        try {
            eQuery = "u_cloud_provider=" + provider + "^u_division=" + deptSysId + "^u_active=true^u_account_identifierIN" + account_id.toString();
            gr_cloud_roles.addEncodedQuery(eQuery);
            gr_cloud_roles.query();
            while (gr_cloud_roles.next()) {
                roleList.push(gr_cloud_roles.u_role_identifier + "");
            }


        } catch (e) {
            gs.error("Exception occurred while getting the list of role ids: " + e.message);
        }

        return roleList.join(",");
Client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	
    var provider;
    var c_AWS_CATEGORY_NAME = "AWS Users: Role Based Access";
    var c_AZR_CATEGORY_NAME = "Azure Users: Role Based Access";
    var c_GOOGLE_NAME = "Google Users : Role Based Access";
	var division = g_form.getValue("department");

    if (this.NOW.category == c_AWS_CATEGORY_NAME) {
        provider = "aws";
    }

    if (this.NOW.category == c_AZR_CATEGORY_NAME) {
        provider = "azure";
    }
    if (this.NOW.category == c_GOOGLE_NAME) {
        provider = "gcp";
    }

	
    //Type appropriate comment here, and begin script below
    var getRole = new GlideAjax('UtilityCoreGlobalClient');
    getRole.addParam('sysparm_name', 'getAccountRoles');
    getRole.addParam('sysparm_acc_id', newValue);
	getRole.addParam('sysparm_provider',provider);
	getRole.addParam('sysparm_deptSysId',division);

    getRole.getXML(fetchRoles);

    function fetchRoles(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		alert(answer);
        var roles = [];
		g_form.clearOptions('role');
		g_form.addOption('role', '', '-- None --');
        
		if(answer){
			g_form.clearOptions('role');
			g_form.addOption('role', '', '-- None --');
			roles = answer.split(",");
			for(i in roles){
				g_form.addOption('role', roles[i], roles[i]);
			}
		}else{
			
			g_form.addOption('role', '', '-- None --');
		}
        

    }

}

 

 

 

please help me get the roles of all Accounts selected

1 ACCEPTED SOLUTION

HrishabhKumar
Kilo Sage

Hi @Community Alums ,

 

I have altered your code (Both Script include and Client script) little bit according to my logic, I have highlighted to modified line in the below code.

1) Script include

 getAccountRoles: function() {
        var provider = this.getParameter('sysparm_provider');
        var deptSysId = this.getParameter('sysparm_deptSysId');
        var account_id = this.getParameter('sysparm_acc_id');
        var roleList = [];
        var gr_cloud_roles = new GlideRecord("u_cloud_roles");
        var eQuery = "";
        try {
            eQuery = "u_cloud_provider=" + provider + "^u_division=" + deptSysId + "^u_active=true^u_account_identifierIN" + account_id.toString();
            gr_cloud_roles.addEncodedQuery(eQuery);
            gr_cloud_roles.query();
            while (gr_cloud_roles.next()) {
                // ========================================================
                roleList.push(gr_cloud_roles.u_role_identifier);
                // ========================================================
            }


        } catch (e) {
            gs.error("Exception occurred while getting the list of role ids: " + e.message);
        }
       // ============================================================
        return JSON.stringify(roleList);
       // ============================================================

 

2) client Script

Client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	
    var provider;
    var c_AWS_CATEGORY_NAME = "AWS Users: Role Based Access";
    var c_AZR_CATEGORY_NAME = "Azure Users: Role Based Access";
    var c_GOOGLE_NAME = "Google Users : Role Based Access";
	var division = g_form.getValue("department");

    if (this.NOW.category == c_AWS_CATEGORY_NAME) {
        provider = "aws";
    }

    if (this.NOW.category == c_AZR_CATEGORY_NAME) {
        provider = "azure";
    }
    if (this.NOW.category == c_GOOGLE_NAME) {
        provider = "gcp";
    }

	
    //Type appropriate comment here, and begin script below
    var getRole = new GlideAjax('UtilityCoreGlobalClient');
    getRole.addParam('sysparm_name', 'getAccountRoles');
    getRole.addParam('sysparm_acc_id', newValue);
	getRole.addParam('sysparm_provider',provider);
	getRole.addParam('sysparm_deptSysId',division);

    getRole.getXML(fetchRoles);

    function fetchRoles(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		alert(answer);
               // ===============================================
                var roles = JSON.parse(answer);
              // ===============================================
        
		g_form.clearOptions('role');
		g_form.addOption('role', '', '-- None --');
        
		if(answer){
			g_form.clearOptions('role');
			g_form.addOption('role', '', '-- None --');
			//roles = answer.split(","); 
                       //==================================================
			for(var i=0;i<roles.length;i++){
                             g_form.addOption('role', roles[i], roles[i]);
                        }
                        //==================================================
		}else{	
			g_form.addOption('role', '', '-- None --');
		}
        

    }

}

 

Hope this helps you, If not then let me know the following things:

1) What is exactly happening to the choice field after to select the Account ID.

2) Give me the screenshot of the form with both fields "Account ID"(Multiple choice) and "Roles"(Dropsdown) 

View solution in original post

3 REPLIES 3

Amitoj Wadhera
Kilo Sage

Hi @Community Alums ,

 

Update the getAccountRoles function to handle multiple Account IDs properly. Specifically, you should join the account IDs into a string that can be used in the query.

 

getAccountRoles: function() {
    var provider = this.getParameter('sysparm_provider');
    var deptSysId = this.getParameter('sysparm_deptSysId');
    var account_ids = this.getParameter('sysparm_acc_id').split(',');
    var roleList = [];
    var gr_cloud_roles = new GlideRecord("u_cloud_roles");
    var eQuery = "";
    try {
        eQuery = "u_cloud_provider=" + provider + "^u_division=" + deptSysId + "^u_active=true^u_account_identifierIN" + account_ids.join(',');
        gr_cloud_roles.addEncodedQuery(eQuery);
        gr_cloud_roles.query();
        while (gr_cloud_roles.next()) {
            roleList.push(gr_cloud_roles.u_role_identifier + "");
        }
    } catch (e) {
        gs.error("Exception occurred while getting the list of role ids: " + e.message);
    }

    return roleList.join(",");
}

 

 

Update the client script to handle multiple selected Account IDs and pass them as a comma-separated string to the GlideAjax call.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var provider;
    var c_AWS_CATEGORY_NAME = "AWS Users: Role Based Access";
    var c_AZR_CATEGORY_NAME = "Azure Users: Role Based Access";
    var c_GOOGLE_NAME = "Google Users : Role Based Access";
    var division = g_form.getValue("department");

    if (this.NOW.category == c_AWS_CATEGORY_NAME) {
        provider = "aws";
    }

    if (this.NOW.category == c_AZR_CATEGORY_NAME) {
        provider = "azure";
    }
    if (this.NOW.category == c_GOOGLE_NAME) {
        provider = "gcp";
    }

    var selectedAccountIDs = g_form.getValue('account_id').split(',');

    var getRole = new GlideAjax('UtilityCoreGlobalClient');
    getRole.addParam('sysparm_name', 'getAccountRoles');
    getRole.addParam('sysparm_acc_id', selectedAccountIDs.join(','));
    getRole.addParam('sysparm_provider', provider);
    getRole.addParam('sysparm_deptSysId', division);

    getRole.getXML(fetchRoles);

    function fetchRoles(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);
        var roles = [];
        g_form.clearOptions('role');
        g_form.addOption('role', '', '-- None --');

        if (answer) {
            g_form.clearOptions('role');
            g_form.addOption('role', '', '-- None --');
            roles = answer.split(",");
            for (var i in roles) {
                g_form.addOption('role', roles[i], roles[i]);
            }
        } else {
            g_form.addOption('role', '', '-- None --');
        }
    }
}

 

By ensuring that the account IDs are correctly formatted and handled as a comma-separated string, both in the client script and the server-side script, you can retrieve the roles for all selected accounts.

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera

Brad Bowman
Kilo Patron
Kilo Patron

The value of a List field or List Collector variable is a comma-separated string of sys_ids, which is what needs to be passed to the server, and used in the GlideRecord as is, so your approach looks correct.  The only line I would change is

roleList.push(gr_cloud_roles.u_role_identifier.toString());

for a more standard method, those yours is probably doing the same thing, forcing the sys_id to a string.

 

This is a simple array, not JSON, so ignore that approach.

 

Does this work once the first selection is made in the List Collector, before any additional selections are made?  Are you getting the expected answer alert with the first and/or subsequent selections?  Add some gs.info or gs,addInfoMessage lines to the Script Include so that you can confirm the values being passed between client and server, and how far the SI is making it, then you will see where it is going wrong.

HrishabhKumar
Kilo Sage

Hi @Community Alums ,

 

I have altered your code (Both Script include and Client script) little bit according to my logic, I have highlighted to modified line in the below code.

1) Script include

 getAccountRoles: function() {
        var provider = this.getParameter('sysparm_provider');
        var deptSysId = this.getParameter('sysparm_deptSysId');
        var account_id = this.getParameter('sysparm_acc_id');
        var roleList = [];
        var gr_cloud_roles = new GlideRecord("u_cloud_roles");
        var eQuery = "";
        try {
            eQuery = "u_cloud_provider=" + provider + "^u_division=" + deptSysId + "^u_active=true^u_account_identifierIN" + account_id.toString();
            gr_cloud_roles.addEncodedQuery(eQuery);
            gr_cloud_roles.query();
            while (gr_cloud_roles.next()) {
                // ========================================================
                roleList.push(gr_cloud_roles.u_role_identifier);
                // ========================================================
            }


        } catch (e) {
            gs.error("Exception occurred while getting the list of role ids: " + e.message);
        }
       // ============================================================
        return JSON.stringify(roleList);
       // ============================================================

 

2) client Script

Client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	
    var provider;
    var c_AWS_CATEGORY_NAME = "AWS Users: Role Based Access";
    var c_AZR_CATEGORY_NAME = "Azure Users: Role Based Access";
    var c_GOOGLE_NAME = "Google Users : Role Based Access";
	var division = g_form.getValue("department");

    if (this.NOW.category == c_AWS_CATEGORY_NAME) {
        provider = "aws";
    }

    if (this.NOW.category == c_AZR_CATEGORY_NAME) {
        provider = "azure";
    }
    if (this.NOW.category == c_GOOGLE_NAME) {
        provider = "gcp";
    }

	
    //Type appropriate comment here, and begin script below
    var getRole = new GlideAjax('UtilityCoreGlobalClient');
    getRole.addParam('sysparm_name', 'getAccountRoles');
    getRole.addParam('sysparm_acc_id', newValue);
	getRole.addParam('sysparm_provider',provider);
	getRole.addParam('sysparm_deptSysId',division);

    getRole.getXML(fetchRoles);

    function fetchRoles(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		alert(answer);
               // ===============================================
                var roles = JSON.parse(answer);
              // ===============================================
        
		g_form.clearOptions('role');
		g_form.addOption('role', '', '-- None --');
        
		if(answer){
			g_form.clearOptions('role');
			g_form.addOption('role', '', '-- None --');
			//roles = answer.split(","); 
                       //==================================================
			for(var i=0;i<roles.length;i++){
                             g_form.addOption('role', roles[i], roles[i]);
                        }
                        //==================================================
		}else{	
			g_form.addOption('role', '', '-- None --');
		}
        

    }

}

 

Hope this helps you, If not then let me know the following things:

1) What is exactly happening to the choice field after to select the Account ID.

2) Give me the screenshot of the form with both fields "Account ID"(Multiple choice) and "Roles"(Dropsdown)