Help on replacing the values through Business rules

Aswin Chandras1
Tera Contributor

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.

 

4 REPLIES 4

Bhavya11
Kilo Patron

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 

If this information proves useful, kindly mark it as helpful or accepted solution.

Thanks,
BK

vaishali231
Kilo Sage

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

 

 

 

Ankur Bawiskar
Tera Patron

@Aswin Chandras1 

so what script did you try and where are you stuck?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Tanushree Maiti
Mega Patron

Hi @Aswin Chandras1 

 

Use this script (it is for BR)

 

var loggedinUserEmail = current.u_logged_in_user;
            var loggedinUserEmailSplit = loggedinUserEmail.split('@');
            var loggedinUserEmailSplitb4atDrate = loggedinUserEmailSplit[0] + '@example.com';
            var loggedinUserEmailSplitb4atDrate1 = loggedinUserEmailSplit[0] + '@example.';  // sometime co.in is also there, avoid it if you dont have these kind of data
 
            var userGr = new GlideRecord('sys_user');
            userGr.addEncodedQuery('email=' + loggedinUserEmail + '^ORuser_nameSTARTSWITH' + loggedinUserEmailSplitb4atDrate +  '^ORuser_nameSTARTSWITH' + loggedinUserEmailSplitb4atDrate1 );
            userGr.orderByDesc('sys_created_on');
            userGr.query();
 
            if (userGr.next()) {
                current.user = userGr.sys_id.toString();
            }
 
 
 
Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: