update group info using data source

VSN
Tera Expert

i have a requirement to update the existing groups and group members, and roles by using the transform map and data source..

 

all this information was stores in a Excel, i have to upload that file using data source and update the existing data.

VSN_0-1747066998693.png

Do  i need to create data source and transform maps for different tables like one for Group table, and another for group members table, and another  for Roles table.

1 ACCEPTED SOLUTION

Robert H
Mega Sage

Hello @VSN ,

 

You can do this using a single Data Source with two Transform Maps.

 

The 1st Transform Map would target the Group [sys_user_group] table, and would have field maps for the Name (coalesce enabled) and Manager columns.

 

To add the roles to the groups you need an "onAfter" Transform Script on the first Transform Map:

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

	var roleNames = source.getValue('u_roles').split(',');
	var groupId = target.getUniqueValue();

	roleNames.forEach(roleName => {
		var gr = new GlideRecord('sys_group_has_role');
		gr.addQuery('group', groupId);
		gr.addQuery('role.name', roleName);
		gr.query();
		if (gr.hasNext()) return;
		gr.initialize();
		gr.setValue('group', groupId);
		gr.setDisplayValue('role', roleName);
		gr.insert();
	});
	

})(source, map, log, target);

 

The 2nd Transform Map would target the Group Member [sys_user_grmember] table, and would map the Name and User ID columns (both with coalesce enabled).

 

Regards,

Robert

View solution in original post

9 REPLIES 9

That solution not working and we are not creating new role, we are just adding the existing role to group.

Hello @VSN ,

 

The script is not creating new roles, it adds the roles mentioned in the Roles column of the Excel sheet to the Group.

The solution definitely works, as I have tested it using an Excel sheet that looks exactly like yours before posting it.

 

Regards,

Robert

it was ignoring the values, not inserting 

Ankur Bawiskar
Tera Patron
Tera Patron

@VSN 

approach is already shared by @Robert H . Please start from your side as it will be learning for you as well

you should get details from docs, youtube videos

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

 Script is not working.because we are adding the existing record of group. Not creating the new roles.