Set user based on the dropdown selection

NARENDRA KAMMIL
Tera Contributor

We have a drop down field u_entity  which have a,b,c as options

 

When option a is selected we need to set  user field (reference ) with a hardcoded group A. manager name.

If B is selected we need to set the user field with hardcoded group B. manager name.

 

I know we can do that with client script , can some one help me with the script.

1 ACCEPTED SOLUTION

Jan Cernocky
Tera Guru

Hey man,

do you need to set the behavior in the backend or portal?

 

The best option in this case for me would be:

1. Define the groups in system property (in case you need to change the logic in the future, you don't need to touch the script, just change the property value),

2. Create Display business rule, it runs when the record is being displayed and you can use the server side. I would check read the sysIDs of the groups from the property and do GlideRecord to group table to read the manager and save it to scratchpad (so you would have 3 scratchpads, or better an array or object of managers)

3. Create the onChange client script and use the value from scratchpad based on your u_entity selection.

 

The solution would be, e.g.

1. Define new property, e.g. my_groups in [sys_properties] table, add there 3 sysIDs of  the  groups you need to work with, separate by comma

sysIDa,sysIDb,sysIDc

2. The business rule would be something like this:

var myGroups = gs.getProperty('my_groups');
var myGroupsArr = myGroups.split(',');
var myManagersArr = [];
for (var i=0; i<myGroupsArr.length; i++) {
   var groupGR = new GlideRecord('sys_user_group');
   groupGR.get(myGroupsArr[i]);
   myManagersArr.push(groupGR.getValue('manager'));
{
g_scratchpad.myManagers = myManagersArr;

3. The client script would be something like this:

var myManagersArr = g_scratchpad.myManagers;
var entity = g_form.getValue('u_entity');
var managerID = '';
if(entity == 'a') {
  managerID = myManagersArr [0];  
}
else if (entity == 'b') {
  managerID = myManagersArr [1];
}
else {
  managerID = myManagersArr [2];
}
g_form.setValue('yourmanagerfield', managerID);
}

 

I haven't tested just wrote it from top of my head but that would be the principle.

View solution in original post

8 REPLIES 8

Saurav11
Kilo Patron
Kilo Patron

hello,

 

Assuming that the three choices are the name of the group and the value is also the same i.e if Database is the name of the group and you have configured the choices as below:-

 

Saurav11_0-1665230466606.png

 

Then use the below onchange client script already tested works fine, replace the userfieldname with your user field name 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	
	var gr= new GlideRecord('sys_user_group');
	gr.addQuery('name',newValue);
	gr.query();
	if(gr.next())
		{
			g_form.setValue('userfieldname',gr.manager);
		}
}

 

Please mark my answer as correct based on Impact.

Thanks Saurav , But the drop down is completely different values , just like A, B and C ( which is not user or group ) 

when a user selects A, we need to populate the manager value of a group (hardcoded group) in a different reference field . ( besides glide ajax is there any other way ?) if no , can you pls help me with the script part please .

Hello,

 

Please find the code below just replace it the value and field name with your value and field name

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var entity = g_form.getValue('u_entity');
    var group;
    if (entity == 'valueofoptiona') {
        group = 'sysidofgroupA';
    } else if (entity == 'valueofoptionb') {
        group = 'sysidofgroupB';
    } else {
        group = 'sysidofgroupC';
    }
    var gr = new GlideRecord('sys_user_group');
    gr.addQuery('sys_id', group);
    gr.query();
    if (gr.next()) {

        g_form.setValue('userfieldname', gr.manager);
    }

}

 

 

Please mark my answer as correct based on Impact.

Firstly , thanks for the script , Will Glide record works on CS ? Seems like the manager is not passing on.