How to check email address from a string field and fetch from sys_user table and populate name

Amol Pawar
Tera Guru

Hi Experts,

I want to populate the user's name in a field named 'Responsible Person' which we have to check from the 'Email Address' field on a record producer's multi row variable set.

AmolPawar_0-1741255929732.png

In above image, Email Id is a string field. We have to check that email id from sys_user table and if it's there, we have to populate that user's name in the next field which is 'Responsible person'.

 

Let me know the approach and possible solution on it.

Thanks in advance,

Amol Pawar

1 ACCEPTED SOLUTION

@Amol Pawar 

Please adjust your function to:

getUserName: function() {
        var email = this.getParameter('sysparm_email');
        var user = new GlideRecord('sys_user');
        user.addQuery('email', email);
        user.query();
        if (user.next()) {
            return user.getDisplayValue('name');
        }
        return '';
    },

If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

View solution in original post

8 REPLIES 8

@Amol Pawar 

Glad to help.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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

Hi Amol,
I hope you are doing well!
Catelog Client script:
You need Create an ‘onChange’ Client Script on the Multi-Row Variable Set
Nav to Catalog Client Scripts > New.
Set the UI Type to All.
Apply it to your Multi-Row Variable Set.
Use the following script:
  var email = newValue;
    var ga = new GlideAjax('UserLookup'); // Call Script Include
    ga.addParam('sysparm_name', 'getUserName');
    ga.addParam('sysparm_email', email);//You can directly give it here and used this one as well g_form.getValue(email);.
    ga.getXMLAnswer(function(response) {
        if (response) {
            g_form.setValue('responsible_person', response);
        } else {
            g_form.clearValue('responsible_person');
            g_form.showFieldMsg('email_id', 'No matching user found.', 'error');
        }
    });
 
Server side script (script include):
 getUserName: function() {
        var email = this.getParameter('sysparm_email');
        var userRec = new GlideRecord('sys_user');
        userRec.addQuery('email', email);
        userRec.query();
        if (userRec.next()) {
            return userRec.getValue('name'); // Fetch user name if its not working instead of this you can use this one(userRec.name.getDisplayvalue() )
        }
        return '';
    }
I hope this one its working. let me know if you faced any issues.

Ankur Bawiskar
Tera Patron
Tera Patron

@Amol Pawar 

why are you not putting Reference variable within that MRVS?

For your current requirement you can use GlideRecord with callback

OR

Enhance your script include as this, don't use initialize() method

var GetUserNameFromEmail = Class.create();
GetUserNameFromEmail.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    getUserName: function(email) {
        var user = new GlideRecord('sys_user');
        user.addQuery('email', email);
        user.query();
        if (user.next()) {
            return user.getDisplayValue('name');
        }
        return '';
    },

    type: 'GetUserNameFromEmail'
});

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

@Amol Pawar 

Thank you for marking my response as helpful.

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