How to fetch list of users where email before @ is not same as UPN (User Principal Name) before @

Priya Singh 2 2
Tera Contributor

How to fetch list of users where email before @ is not same as UPN (User Principal Name) before @

 

I want to fetch list of users where email before @ is not same as UPN before @ on user table but I am not able to achieve it by background script, it's returning all the users, please advise how I can get this data ?

7 REPLIES 7

Ok.. It seems it similar to our issue , you are facing..

 

Sharing you the code. base format already shared. only few addition is required.

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Yes please share if you have a solution for this 

Hi @Priya Singh 2 2 

 

try with this 

 

(function executeRule(current, previous /*null when async*/ ) {
    var op = current.operation();
 
     if (op == "update") {
        if (current.user_principal_name && current.user == '') {         
            var upnp = current.user_principal_name;
            var partUpn = upnp.split('@');
            var partUpnEmailp = partUpn[0] + '@esc.com';
            var partUpnEmailp1 = partUpn[0] + '@esc.';   // In our case all domain was NOT part of exact domain (for your case esc.com) ,few was like abc.co.
 
            var userGrp = new GlideRecord('sys_user');
            userGrp.addEncodedQuery('email=' + upnp +'^ORemailSTARTSWITH' + partUpnEmailp + '^ORemailSTARTSWITH' + partUpnEmailp1 );
            userGrp.orderByDesc('sys_created_on');
            userGrp.query();
 
            if (userGrp.next()) {
                current.user = userGrp.sys_id.toString();
            }
        } else {
 
            var upn = current.user_principal_name;
            var partUpn = upnp.split('@');
            var partUpnEmailp = partUpn[0] + '@esc.com';
            var partUpnEmailp1 = partUpn[0] + '@esc.';   // In our case all domain was part of esc.com ,few was esc.co.
 
            var userGrp = new GlideRecord('sys_user');
            userGrp.addEncodedQuery('email=' + upn +'^ORemailSTARTSWITH' + partUpnEmailp + '^ORemailSTARTSWITH' + partUpnEmailp1 );
            userGrp.orderByDesc('sys_created_on');
            userGrp.query();
 
            if (userGr.next()) {
                current.user = userGr.sys_id.toString();
            }
        }
    }
})(current, previous);
 
 
Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: