Help on replacing the values through Business rules
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi All,
I have a field called logged in user in computer table which is updated through discovery as an email id not as an user id.
We are copying the email id and trimming the same user id within servicenow and comparing with sys_user table and copying the value in the field called frequent user.
Now the ask is if the logged in user gets updated in the field I need to update in the frequent user field through business rule. How to achieve this.
I need to trim and get only the user id. The example value in logged in user would be 12345@example.com. In this 12345 is the user id available in the user table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Aswin Chandras1 ,
you need to write business rule on computer table[when to run before insert/update] with condition that whenever logged in user get changed and logged in user is not empty
then you need to write script
var loggedInEmail = current.getValue('u_logged_in_user'); // your field name here. here current is computer table object.
if (loggedInEmail) {
var targetID = loggedInEmail.split('@')[0];
var userGR = new GlideRecord('sys_user');
userGR.addQuery('user_name', targetID);
userGR.query();
if (userGR.next()) {
// Update the 'Frequent user' field ON THE COMPUTER table
current.u_frequent_user = userGR.getValue('user_name');
}
}
please try this
Thanks,
BK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi Bhavya,
I got a slight change in requirement now as there was a design change. I will get an value in the last logged in user field I need to trim that and need to check the same in user_id in the user table. If value is there discovery team handles to populate in the frequent user field.
If the value is not present in user_id then it checks with RF ID in user table. Now my requirement is if the value is not present/unavailable in the user table in both user_id and RF ID I need to clear the value from most frequent logged in user in computer table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Aswin Chandras1 ,
For that you little tweak in this Business rule.
var loggedInEmail = current.getValue('u_logged_in_user'); // your field name here. here current is computer table object.
if (!loggedInEmail) {
current.u_most_frequent_logged_in_user = '';
return;
}
var userFound = false;
var userGR = new GlideRecord('sys_user');
// Check user_name
userGR.addQuery('user_name', extractedId);
userGR.query();
if (userGR.next()) {
userFound = true;
} else {
// Check RF ID
var userRF = new GlideRecord('sys_user');
userRF.addQuery('user_name', current.rf_id); // replace with correct RF ID field
userRF.query();
if (userRF.next()) {
userFound = true;
}
}
// If NOT found in both → clear field
if (!userFound) {
current.u_most_frequent_logged_in_user = '';
}
Thanks,
BK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hey @Aswin Chandras1
Create a Before Update Business rule on Table: cmdb_ci_computer
(function executeRule(current, previous /*null when async*/) {
// Run only if logged in user changed
if (current.u_logged_in_user.changes()) {
// Get email value
var email = current.u_logged_in_user.toString();
if (email) {
// Extract value before @
var userId = email.split('@')[0];
// Query sys_user table
var userGR = new GlideRecord('sys_user');
userGR.addQuery('user_name', userId);
userGR.query();
if (userGR.next()) {
// Update frequent user reference field
current.u_frequent_user = userGR.sys_id;
} else {
// Clear field if user not found
current.u_frequent_user = '';
}
}
}
})(current, previous);
*************************************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.
Regards
Vaishali Singh
Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb