AJX script to Auto Populate UserID

Annette Kitzmil
Tera Guru

Hello,

I have a requirement to auto fill the user fields based on a reference field on our table.  I am still missing something because it shows the answer of Null and I am not see why.  Below are my two scripts.

 

AJAX / Script Include:

 

function onLoad(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
        return;
    }
   
    var ga = new GlideAjax('clientUtilsCustom');
      ga.addParam('sysparm_name', 'getUserLanID');//this calls the AJAX script
      //ga.addParam('sysparm_LanID', '');//this would be for the user_name which is the LanID
      ga.getXML(getResp);
 
}
 
function getResp(response) {
    alert(response.responseXML.documentElement.getAttribute("answer"));
    g_form.setValue('user_id',response.responseXML.documentElement.getAttribute("answer"));
   
}

 

Client Script:

function onLoad(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
        return;
    }
   
    var ga = new GlideAjax('clientUtilsCustom');
      ga.addParam('sysparm_name', 'getUserLanID');//this calls the AJAX script
      //ga.addParam('sysparm_LanID', '');//this would be for the user_name which is the LanID
      ga.getXML(getResp);
 
}
 
Thank you for any assistance on this.
function getResp(response) {
    alert(response.responseXML.documentElement.getAttribute("answer"));
    g_form.setValue('user_id',response.responseXML.documentElement.getAttribute("answer"));
   
}
1 ACCEPTED SOLUTION

Here is the final client script that worked and ended up needed a condition added that I didn't realize until I was testing.

 

Client Script:

    var ga = new GlideAjax('clientUtilsCustom');
    var formUser = g_form.getValue('user_id');
   
    if (!formUser) {
        //alert(':' + formUser + ':');
        ga.addParam('sysparm_name', 'getUserLanID'); //this calls the AJAX script
        ga.getXML(getResp);

    }

}

function getResp(response) {

    var answer = response.responseXML.documentElement.getAttribute("answer");
    //alert(':' + formUser + ':');
    g_form.setValue('user_id', answer);    

}
 
This is the AJX on line of code that was needed:
 
getUserLanID: function() {
       
        return gs.getUser().getName();      
       
    },
 

View solution in original post

7 REPLIES 7

Sandeep Rajput
Tera Patron
Tera Patron

@Annette Kitzmil Instead of posting the script include code, you posted client script twice. Can you provide the code of script include. 

My apologies...

 

getUserLanID: function() {
        var LanID = this.getParameter('sysparm_LanID');
        LanID = gs.getUser().getRecord().getValue('user_name');
        var openedBy = new GlideRecord('sys_user');
        openedBy.addQuery('user_id', LanID);
        openedBy.query();
        if (openedBy.next())
            gs.addInfoMessage('AK: ' + LanID);
        return LanID;

    },

Hi @Annette Kitzmil ,

 

Please try below code:-

 

Server side code

getUserLanID: function() {
        var LanID = this.getParameter('LanID');
        var openedBy = new GlideRecord('sys_user');
        openedBy.addQuery('user_id', LanID);
        openedBy.query();
        if (openedBy.next())     
        return (JSON.stringify(openedBy);
    },

  client side code:-

function onLoad(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
        return;
    }
   
    var ga = new GlideAjax('clientUtilsCustom');
      ga.addParam('sysparm_name', 'getUserLanID');//this calls the AJAX script
      ga.addParam('sysparm_LanID', '');//this would be for the user_name which is the LanID
      ga.getXML(getResp);
 
}
function getResp(response) {
    var answer=response.responseXML.documentElement.getAttribute("answer"));
      answer= JSON.parse(answer);
    g_Form.setValue('name', answer.name)
   
}

 

Regards,

Ranjit

Very close, now I get this:

AnnetteKitzmil_0-1714498720839.png