Script include for reference Qualifier

Gokul Nath
Kilo Sage

Hi all,

I am stuck with an requirement of displaying dynamic values using a reference field based on user groups.

I need to change the drop downs as per the variable selected by the user in the record producer.

Example: If a user selects a American entity, the drop down should show up approvers from american approval group, and in case if user chooses from Pacific region, it should take users from the pacific approval group.


I created a script include and called the function in advanced qualifier. Not sure if the logic is right. the drop down always enters the else loop.

Please find the code below and drop your suggestions

 

 

function dynamicapprovers() {


    //var entity = producer.u_roc;// your assignment_group variable name
	
	var region = producer.entity_id.u_region;
    var users = '';
    if (region == "APAC") {
        var getAPACMembers = new GlideRecord('sys_user_grmember');
        getAPACMembers.addQuery('group.name', "APAC Approval Group");
        getAPACMembers.query();
        while (getAPACMembers.next()) {
            users = users + ',' + getAPACMembers.user;
        }
        return 'sys_idIN' + users;
	}
    else if (region == "NAM"){
        var getAMERMembers = new GlideRecord('sys_user_grmember');
        getAMERMembers.addQuery('group.name', "North America Approval Group");
        getAMERMembers.query();
        while (getAMERMembers.next()) {
            users = users + ',' + getAMERMembers.user;
        }
        return 'sys_idIN' + users;
    }
	
	
	
	

}

 

 

 

454545.png

2 ACCEPTED SOLUTIONS

Ratnakar7
Mega Sage
Mega Sage

Hi @Gokul Nath  ,

There are a few issues in your script include that may be causing the problem.

Firstly, the variable producer is not defined in the script include. It looks like you are trying to access the value of a variable called u_region from the record producer, but this is not defined in the script include. You will need to pass the value of u_region from the record producer to the script include function as a parameter.

Secondly, you are returning a string in the format of sys_idIN<user_sys_id> for each user in the approval group. This is not a valid reference qualifier format. You should be returning an encoded query string that filters the sys_user table to only show the desired users.

Here is an updated version of your script include that addresses these issues:

 

function dynamicapprovers(region) {
    var users = [];
    if (region == "APAC") {
        var getAPACMembers = new GlideRecord('sys_user_grmember');
        getAPACMembers.addQuery('group.name', "APAC Approval Group");
        getAPACMembers.query();
        while (getAPACMembers.next()) {
            users.push(getAPACMembers.user);
        }
    }
    else if (region == "NAM"){
        var getAMERMembers = new GlideRecord('sys_user_grmember');
        getAMERMembers.addQuery('group.name', "North America Approval Group");
        getAMERMembers.query();
        while (getAMERMembers.next()) {
            users.push(getAMERMembers.user);
        }
    }
    var encodedQuery = 'sys_idIN' + users.join(',');
    return encodedQuery;
}

 

In your record producer, you will need to pass the value of u_region to the script include function like this:

 

javascript&colon; dynamicapprovers(current.variables.entity_id.u_region)

 

 

If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.

 

Thank you!

Ratnakar

View solution in original post

Hello @Ratnakar7 

Thanks for the time.

Your solution helped a lot.
But the dropdown is only returning a single user, but my group holds multiple users. Please help with this.
Also my region field is a reference field which is working now by using sys_id in if case , is there a way by which i can return display value to the parameter 

Gokul24_0-1679987855512.png

 

View solution in original post

3 REPLIES 3

Ratnakar7
Mega Sage
Mega Sage

Hi @Gokul Nath  ,

There are a few issues in your script include that may be causing the problem.

Firstly, the variable producer is not defined in the script include. It looks like you are trying to access the value of a variable called u_region from the record producer, but this is not defined in the script include. You will need to pass the value of u_region from the record producer to the script include function as a parameter.

Secondly, you are returning a string in the format of sys_idIN<user_sys_id> for each user in the approval group. This is not a valid reference qualifier format. You should be returning an encoded query string that filters the sys_user table to only show the desired users.

Here is an updated version of your script include that addresses these issues:

 

function dynamicapprovers(region) {
    var users = [];
    if (region == "APAC") {
        var getAPACMembers = new GlideRecord('sys_user_grmember');
        getAPACMembers.addQuery('group.name', "APAC Approval Group");
        getAPACMembers.query();
        while (getAPACMembers.next()) {
            users.push(getAPACMembers.user);
        }
    }
    else if (region == "NAM"){
        var getAMERMembers = new GlideRecord('sys_user_grmember');
        getAMERMembers.addQuery('group.name', "North America Approval Group");
        getAMERMembers.query();
        while (getAMERMembers.next()) {
            users.push(getAMERMembers.user);
        }
    }
    var encodedQuery = 'sys_idIN' + users.join(',');
    return encodedQuery;
}

 

In your record producer, you will need to pass the value of u_region to the script include function like this:

 

javascript&colon; dynamicapprovers(current.variables.entity_id.u_region)

 

 

If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.

 

Thank you!

Ratnakar

Hello @Ratnakar7 

Thanks for the time.

Your solution helped a lot.
But the dropdown is only returning a single user, but my group holds multiple users. Please help with this.
Also my region field is a reference field which is working now by using sys_id in if case , is there a way by which i can return display value to the parameter 

Gokul24_0-1679987855512.png

 

Hi @Gokul Nath  ,

To construct the encoded query with multiple values from the users array, we are using the Array.join() method to concatenate the array elements into a string separated by commas.

please print the value of 'encodedQuery' var into logs, and very the same:

 

var encodedQuery = 'sys_idIN' + users.join(',');
gs.log('dynamicapprovers_encoded_query:'+encodedQuery);
    return encodedQuery;

 

 This will create a string that looks like sys_idIN<sys_id1>,<sys_id2>,<sys_id3>,... where <sys_idX> represents the sys_id values of each user in the users array.

 

Regarding your second question, to display the display value instead of sys_id, you can use the getDisplayValue() method of the reference field.
Here is the example:

 

javascript&colon;dynamicapprovers(current.variables.entity_id.u_region.getDisplayValue())

 

OR

use dot walking like below:

 

 

javascript&colon;dynamicapprovers(current.variables.entity_id.u_region.name.toString());

 

 

 

 

Thanks,

Ratnakar