- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2023 09:51 AM
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
Solved! Go to Solution.
- Labels:
-
ITSM: General
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2023 10:24 AM
here are a few issues with the current implementation of your business rule:
- 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.
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2023 10:24 AM
here are a few issues with the current implementation of your business rule:
- 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.
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-13-2023 02:12 AM