How to populate the logged in user to a variable from the Onload Client script

kavitha_cr
Mega Guru

Hi All,

Requirement: 

On the load of the form, it should populate the logged in user name, company, email Id in the variables on the form level,

I have written a Onload client script to populate the company and email details but I'm unable to get the logged in user name to the field.

Below are scripts

Script Include:

var microsoft_teams_external_calling = Class.create();
microsoft_teams_external_calling.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
    getEmployeeDetails: function() {
            var requested_for = this.getParameter('sysparm_user_name');
            var result = this.newItem("result");
            var grp1 = new GlideRecord('sys_user');
            grp1.addQuery('sys_id', requested_for);
            grp1.query();
            if(grp1.next()){
                
                result.setAttribute("usserName", grp1.user_name); 
                result.setAttribute("emailID", grp1.email); 
                result.setAttribute("companyName", grp1.company);
            
            return result;
    }
    },
    type: 'microsoft_teams_external_calling'
});

Client Script:

function onLoad() {
    
    var requested_for = g_form.getValue('requested_for');
    var ga = new GlideAjax('microsoft_teams_external_calling');
    ga.addParam('sysparm_name', 'getEmployeeDetails');
    ga.addParam('sysparm_user_name', requested_for);
    ga.getXML(Displayresult);

    function Displayresult(response) {
        var result = response.responseXML.getElementsByTagName("result");

var email_ID = result[0].getAttribute("emailID");
        var Company     = result[0].getAttribute("companyName");
        var UserN  = result[0].getAttribute("usserName");

        g_form.setValue('email', email_ID);
        g_form.setValue('company',Company);
        g_form.setValue('name',UserN);
     
    }
}
    To get the Logged in user and based on that to populate the name, company and email details is what I'm not getting, Can anyone please guide me.

Thanks,

Siva Jyothi.    
 

10 REPLIES 10

Tony Chatfield1
Kilo Patron

Hi, if this is running in an onLoad() client script, then the expectation would be that requested_for would be NULL unless a value is set earlier in this onLoad() script or in an onload script that is running before it.
On client side you can use g_user.userID to get user sys_id value based on logged in user and then pass this value as parameter to your server script 

GlideUser | ServiceNow Developers