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.

 

10 REPLIES 10

Hi Vaishali,

 

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.

 

 

Hey @Aswin Chandras1 

Try this :

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

    if (current.u_last_logged_in_user.changes()) {

        var loginValue = current.u_last_logged_in_user.toString();

        if (loginValue) {

            // Trim value before @
            var userValue = loginValue.split('@')[0];

            var userGR = new GlideRecord('sys_user');

            // Check user_id OR RF ID
            userGR.addQuery('user_id', userValue);
            userGR.addOrCondition('u_rf_id', userValue);

            userGR.query();

            if (!userGR.next()) {

                // Clear field if no matching user found
                current.u_most_frequent_logged_in_user = '';
            }
        } else {

            // Clear field if source field is empty
            current.u_most_frequent_logged_in_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





hey @Aswin Chandras1 

Hope you are doing well.

Did my previous reply answer your question?

If it was helpful, please mark it as correct ✓ and close the thread . This will help other readers find the solution more easily.

 

Thankyou & Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb






Goka Srinu
Tera Contributor

Write a before BR and keep the condition as "Looged In user changes & Logged In User is not empty". and write a script as below:

var loggedInUser = current.u_logged_in_user.toString();  
var userIdForFreqUser =loggedInUser.split("@")[0];

    if (userIdForFreqUser) {
     
            current.u_frequent_user = loggedInUser;
   
    }

  

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