Prevent duplicate records in sys_user_grmember

richardyaxl
Tera Expert

In our ServiceNow instance, I have noticed that it is possible to insert duplicate records into the sys_user_grmember table. However, in my out of the box PDI there seems to be something in place that prevents this, and throws and "Invalid insert" error when trying to add a user to a group that is already a member.

I have looked at Business rules, ACLs, and indexing but I am struggling to identify where this is set so that I can duplicate it. Please can somebody help me to identify how this is configured?

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

Interesting.  It looks like this must have been introduced in Zurich.  I'm not sure if this is another thing ServiceNow is hiding from us, or if it's a new platform-level feature, but we can easily re-create the functionality with a Business Rule that looks like this:

BradBowman_0-1762729178333.png

The Script on the Advanced tab would look like this - with an optional bonus line for a better user experience:

(function executeRule(current, previous /*null when async*/) {
	var grp = new GlideRecord('sys_user_grmember');
	grp.addQuery('user', current.user);
	grp.addQuery('group', current.group);
	grp.query();
	if (grp.next()) {
		gs.addErrorMessage(current.user.name + ' is already a member of ' + current.group.name);
		current.setAbortAction(true);
	}
})(current, previous);

 

  

Philippe Casidy
Tera Guru

Hi,

I have this behavior in Yokohama release.
When I try to insert an already existing member in sys_user_grmember, I got this "Invalid Insert".

 

Looking at the log I can read:

txid=83... SUA_47f3... SEVERE *** ERROR *** FAILED TRYING TO EXECUTE ON CONNECTION glide.. (connpid=3...): INSERT INTO sys_user_grmember (...)
Unique Key violation detected by database ((conn=34.) Duplicate entry 'd7eec1d01b4a2159cc4bcb33-50e5c4fc038d4b...' for key 'group')
java.sql.SQLIntegrityConstraintViolationException: (conn=346..) Duplicate entry 'd7eec1d0159cc4bcb33-50e5c49dbcfc038d4b...' for key 'group'
at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:229)
at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.getException(ExceptionMapper.java:165)
at org.mariadb.jdbc.MariaDbStatement.executeExceptionEpilogue(MariaDbStatement.java:238)

 

I looked at the sys_user_gr_member table and could in see in the related lists:

PhilippeCasidy_0-1762735902862.png

PhilippeCasidy_1-1762735969349.png

 

And I guess, that you cannot have duplicates for the index "group".

 

I have to pause on this for now but would love to read the results of your investigations.

 

(and yes, you can always create a BR if required)

 

Regards,

Philippe