How to add the users post creation in the request item record field

Nagesh5
Tera Contributor

@Ankur Bawiskar 

Hello Community,

 

We have seanrio if we add the email address in the watchlist field we need to check that email address already registered in the user table or not if not we are creating the users by using the below business rule script.

 

 

 

But the challenge is we are expecting the user should be added in the watchlist as user instead of email address in the field.Can anyone help us how to achive this?

 

Thanks,

Nagesh

2 REPLIES 2

Alka_Chaudhary
Mega Sage
Mega Sage

Hello @Nagesh5 ,

To add user in the watchlist instead of email, you can create a new array and push the user sys id in the array. Please refer to the below code (before insert business rule):-

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

    // Add your code here
    var watchListUser = current.watch_list.split(',');
    gs.info('watch' + watchListUser);
    var newwatchList = [];
    for (var i = 0; i < watchListUser.length; i++) {

        gs.info('watch @' + watchListUser[i].contains('@'));
        if (watchListUser[i].contains('@')) {

            var userdetails = new GlideRecord("sys_user");
            userdetails.addQuery("email", watchListUser[i].toString());
            userdetails.query();
            if (!userdetails.next()) {
                userdetails.initialize();
                userdetails.first_name = watchListUser[i];
                userdetails.email = watchListUser[i];
                userdetails.user_name = watchListUser[1];
                var sys = userdetails.insert();
                newwatchList.push(sys.toString());
            }else{
				newwatchList.push(userdetails.sys_id.toString());
			}
        } else {
            newwatchList.push(watchListUser[i].toString());
        }
    }
    current.watch_list = newwatchList.toString();

})(current, previous);

 

Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Alka

Ankur Bawiskar
Tera Patron
Tera Patron

@Nagesh5 

even if you don't create the user it will send email to that email address

why a user has to be created for that email address?

I don't think it's a valid business requirement since Users in sys_users are mostly populated via AD, LDAP, Azure etc

If my response helped please mark it correct and close the thread so that it benefits future readers.

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