Fix script To pull all group names and update automatically on list field

lakshmi_laksh
Tera Contributor

Hello,
I have an requirement where i need to pull all group names based on user on employee profile table to update automatically on list field.
need one time script / Fix script to update these in one short
if anyone can help me on this
Thanks in advance.

 

 

1 ACCEPTED SOLUTION

Nilesh Pol
Tera Guru

Hi @lakshmi_laksh 

use below script in your fix scritp:

(function executeFixScript() {
// Define the user field and the employee profile table
var employeeProfileGR = new GlideRecord('x_your_custom_employee_profile'); // Replace with your actual table name
employeeProfileGR.query();

while (employeeProfileGR.next()) {
var user = employeeProfileGR.getValue('user'); // Adjust the field name as needed
if (!user) continue;

var groupList = [];

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', user);
gr.query();

while (gr.next()) {
var groupName = gr.group.name.toString();
groupList.push(groupName);
}

// Join group names with comma and space, or just comma if that's the requirement
var groupListStr = groupList.join(', ');

// Assuming there is a list field or string field to store group names
employeeProfileGR.setValue('group_list_field', groupListStr); // Replace 'group_list_field' with your field name
employeeProfileGR.update();
}

gs.info('Fix script to update user groups in employee profile completed.');
})();

View solution in original post

3 REPLIES 3

david349baker
Mega Contributor

@lakshmi_laksh wrote:

Hello,
I have an requirement where i need to pull all group names based on user on employee profile table to update automatically on list field.
need one time script / Fix script to update these in one short
if anyone can help me on this
Thanks in advance.

lakshmi_laksh_0-1745909237412.png

 


Hello @lakshmi_laksh,
To achieve this, you can use a script to query the employee profile table and fetch group names based on the user. Then, update the list field dynamically. Ensure the script filters relevant groups and handles updates efficiently. If you're using a platform like ServiceNow, GlideRecord can be helpful for querying and updating records. Let me know if you need a sample script!


Best Regards,
David Baker

Nilesh Pol
Tera Guru

Hi @lakshmi_laksh 

use below script in your fix scritp:

(function executeFixScript() {
// Define the user field and the employee profile table
var employeeProfileGR = new GlideRecord('x_your_custom_employee_profile'); // Replace with your actual table name
employeeProfileGR.query();

while (employeeProfileGR.next()) {
var user = employeeProfileGR.getValue('user'); // Adjust the field name as needed
if (!user) continue;

var groupList = [];

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', user);
gr.query();

while (gr.next()) {
var groupName = gr.group.name.toString();
groupList.push(groupName);
}

// Join group names with comma and space, or just comma if that's the requirement
var groupListStr = groupList.join(', ');

// Assuming there is a list field or string field to store group names
employeeProfileGR.setValue('group_list_field', groupListStr); // Replace 'group_list_field' with your field name
employeeProfileGR.update();
}

gs.info('Fix script to update user groups in employee profile completed.');
})();

lakshmi_laksh
Tera Contributor

Thanks a lot