Auto populate or auto fill the user details based on email address field filled?

mathumathi
Tera Contributor

Hi 
can anyone help me with this task?

How to  Auto-populate the user details based on the email address field is filled?
In brief :
If we fill the Email address field in any form, it should auto-populate the details of that email user.
For example:
In a form After we fill the Email address field as Arav00@Example.com , the first name , last name , phone number, Department, Employee id , location etc.,, fields need to be auto populate or auto filled of the Arav00 user details which is saved in the user information.

Thanks in Advance!!

2 REPLIES 2

Sandeep Rajput
Tera Patron
Tera Patron

@mathumathi You can choose to define an onChange Client script on your Email address field.

Inside your script you can call a Script Include to get the other details related to the user. 

 

Here is an example.

 

Screenshot 2023-10-03 at 10.18.23 PM.png

 

Here is the sample onChange Script.

    //Type appropriate comment here, and begin script below
    var myUtils = new GlideAjax('HRUtils');
    myUtils.addParam('sysparm_name', 'getUserDetails');
    myUtils.addParam('sysparm_email', g_form.getValue('email'));
    myUtils.getXML(subjectPersonResponse);

    function subjectPersonResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var subjectPerson = JSON.parse(answer);

        g_form.setValue('first_name', subjectPerson['user_first_name']);
        g_form.setValue('last_name', subjectPerson['user_last_name']);
    }

}

 

Here is the script include which works behind the scene.

 

Screenshot 2023-10-03 at 10.22.04 PM.png

var HRUtils = Class.create();
HRUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserDetails: function() {

        var email = this.getParameter('sysparm_email');
        var glideUser = new GlideRecord('sys_user');
        var someUser = {};
        if (glideUser.get('email',email)) {

            {
                someUser['first_name'] = glideUser.getValue('first_name');
                someUser['last_name'] = glideUser.getValue('last_name');
            }

        }
        return JSON.stringify(someUser);
    },

    type: 'HRUtils'
});

 

Hope this helps.

Anand Kumar P
Giga Patron
Giga Patron

Hi @mathumathi ,

 

 

 

 

Client script onchange on email field--
var emailget=g_form.getValue('email');
var ga = new GlideAjax('script_includename');
ga.addParam('sysparm_name', 'getUserDetailsByEmail');
ga.addParam('sysparm_email', emailget); 
ga.getXML(function(response) {
    var userDetailsArray = [];
    var responseObj = JSON.parse(response.responseXML.documentElement.getAttribute('answer'));
    if (responseObj && responseObj.length > 0) {
        userDetailsArray = responseObj;
    }
 for (var i = 0; i < userDetailsArray.length; i++) {
            var user = userDetailsArray[i];
            g_form.setValue('first_name', user.first_name);
            g_form.setValue('last_name', user.last_name);
            g_form.setValue('phone', user.phone);
            g_form.setValue('department', user.department);
            g_form.setValue('employee_id', user.employee_id);
            g_form.setValue('location', user.location);
     
        }
    }
});
Script include-
var emailid = this.getParameter('sysparm_email');
    getUserDetailsByEmail: function() {
        var userGr = new GlideRecord('sys_user');
        userGr.addQuery('email', emailid);
        userGr.query();
        var userDetailsArray = [];
        while (userGr.next()) {
            var userDetails = {
                first_name: userGr.first_name,
                last_name: userGr.last_name,
                phone: userGr.phone,
                department: userGr.department,
                employee_id: userGr.employee_id,
                location: userGr.location
            };
            userDetailsArray.push(userDetails);
        }

        return userDetailsArray;
    },

 

 

 

 


Thanks,

Anand