Convert sysid into display value

Sam Motley
Giga Guru

hi all, 

I'm trying to convert the sysid in oldvalue from the sys_audit table into a reference user field.

ive created a reference field (references sys_user) 

 

and then creating a business rule that should take the sysid value from oldvalue and copy to the u_olduser reference field i created. 

Doesn't seem to be pulling any values though. 

 

here's what i have currently for the business rule: 

 

var user = current.getValue("oldvalue");
current.u_olduser = user;
current.update;

 

Thanks for taking the time to review. 

Sam

1 ACCEPTED SOLUTION

Syedmd08
Kilo Guru

here are a few issues with the current implementation of your business rule:

  1. The getValue() function only retrieves the value of the field, but in this case, you need to retrieve the entire record for the user from the sys_user table based on the sys_id value in oldvalue.
  2. The current.update statement should be current.update() to actually save the changes to the record.

Here's an updated version of the business rule that should work:

(function executeRule(current, previous /*null when async*/ ) {

var oldUserId = current.oldvalue;

var userGr = new GlideRecord('sys_user');
userGr.addQuery('sys_id', oldUserId);
userGr.query();

if (userGr.next()) {
current.u_olduser = userGr.getUniqueValue();
current.update();
}

})(current, previous);

 

This updated code retrieves the sys_user record for the user with the sys_id value in oldvalue and sets the u_olduser reference field on the current record to that user record. It then saves the current record with current.update(). Note that the query() function is used to retrieve the record from the sys_user table, and the getUniqueValue() function is used to retrieve the sys_id value of the user record to set the u_olduser field.

 

Please mark the answer as correct if you find useful.

View solution in original post

2 REPLIES 2

Syedmd08
Kilo Guru

here are a few issues with the current implementation of your business rule:

  1. The getValue() function only retrieves the value of the field, but in this case, you need to retrieve the entire record for the user from the sys_user table based on the sys_id value in oldvalue.
  2. The current.update statement should be current.update() to actually save the changes to the record.

Here's an updated version of the business rule that should work:

(function executeRule(current, previous /*null when async*/ ) {

var oldUserId = current.oldvalue;

var userGr = new GlideRecord('sys_user');
userGr.addQuery('sys_id', oldUserId);
userGr.query();

if (userGr.next()) {
current.u_olduser = userGr.getUniqueValue();
current.update();
}

})(current, previous);

 

This updated code retrieves the sys_user record for the user with the sys_id value in oldvalue and sets the u_olduser reference field on the current record to that user record. It then saves the current record with current.update(). Note that the query() function is used to retrieve the record from the sys_user table, and the getUniqueValue() function is used to retrieve the sys_id value of the user record to set the u_olduser field.

 

Please mark the answer as correct if you find useful.

Thanks @Syedmd08 that worked a treat, i appreciate you taking the time to review 

 

have a good week!