Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Get Logged in user location in short description using OnLoad client script

somadileep840
Giga Contributor

Hi ,
Need help on getting logged in user location in short description using onload client script using GlideAjax.

somadileep840_1-1737620442542.png

somadileep840_2-1737620515035.png

 


Thanks in Advance,
Dileep

2 REPLIES 2

Shruti
Giga Sage

Hi,

In client script line 6 replace g_user.location with g_user.userID

In script include - paste below code within the function

var result ='';

var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id',this.getParameter('sysparm_locname'));
grUser.query();
if(grUser.next()){
    result = grUser.location.getDisplayValue());
}
return result;

 

 

somadileep840
Giga Contributor

Client script:

function onLoad() {
   
   
 var ga = new GlideAjax('LocationScriptinclude');
 ga.addParam('sysparm_name' ,'getuserlocation');
 ga.addParam('sysparm_locname','g_user.userID');
 ga.getXML(callbackfunction);

 function callbackfunction(response)
 {
    var answer=response.responseXML.documentElement.getAttribute("answer");
    alert(answer);
    g_form.setValue('short_description'+" " +answer);
 }
}
Script Include:
var LocationScriptinclude = Class.create();
LocationScriptinclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

   
   getuserlocation : function()
   {
    var result='';
    var gr= new GlideRecord('sys_user');
    gr.addQuery('sys_id',this.getParameter('sysparm_locname'));
    gr.query();
    if(gr.next()){
        result=gr.location.getDisplayValue();
    }
    return result;
   },
   type: 'LocationScriptinclude'
});
still Alert showing empty