Help on replacing the values through Business rules

Aswin Chandras1
Tera Contributor

HI All

 I will get an value in the last logged in user field in computer table I need to trim that and need to check the same in user_id in the user table. Example value is: abc@example.com. ABC is the user id value or RF ID in user table.

 

If the value is not present in user_id then I need to check RF ID value 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.

8 REPLIES 8

@Aswin Chandras1 

update your BR script as this

Try to add logs as per your preference to debug

(function executeRule(current, previous) {

    if (!current.most_frequent_user.changes())
        return;

    var v = current.most_frequent_user.toString().trim();
    var key = v ? v.split('@')[0].trim() : '';
    var usr = new GlideRecord('sys_user');

    if (!key) {
        current.setValue('u_last_logged_in_user', '');
        current.setValue('most_frequent_user', '');
        return;
    }

    usr.addQuery('user_id', key).addOrCondition('u_rf_id', key); // replace u_rf_id with your RF ID field
    usr.setLimit(1);
    usr.query();

    if (usr.next())
        current.setValue('u_last_logged_in_user', usr.getUniqueValue());
    else {
        current.setValue('u_last_logged_in_user', '');
        current.setValue('most_frequent_user', '');
    }

})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Tanushree Maiti
Tera Patron

Hi @Aswin Chandras1 

 

Try this code snippet from BR. It will work. Update query with correct field name as per your instance.

 

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_idSTARTSWITH' + loggedinUserEmailSplitb4atDrate + '^ORuser_idSTARTSWITH' + loggedinUserEmailSplitb4atDrate1 + '^ORu_rf_id=' + loggedinUserEmailSplitb4atDrate + '^ORu_rf_id=' + loggedinUserEmailSplitb4atDrate1 );
userGr.orderByDesc('sys_created_on');
userGr.query();

if (userGr.next()) {
current.user = userGr.sys_id.toString();
}

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Hi @Tanushree Maiti :

I need to check the RF ID and user id from user table based on the value populated in last logged in user. if value is not present in either of the field I need to clear the value from frequent logged in user in computer table

Hi @Aswin Chandras1 

 

In that case,

 

In query , just search by email in user table ( modify the query) . 

When it is matching the record , and returning the result , at last line -check  your value using appending two line. Use the value wherever you want.

 

gs.log ( userGr.user_id.toString());

gs.log ( userGr.u_rf_id.toString());

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti