Help on replacing the values through Business rules
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
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
yesterday
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
yesterday
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
so what script did you try and where are you stuck?
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Use this script (it is for BR)
