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

@Annette Kitzmil Update the script include method as follows.

 

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

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();      
       
    },
 

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Any reason you are doing this with an onLoad Client Script and not with just using default value on the field?

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn