How can I pass information from g._user in client script?

Lon Landry2
Tera Contributor

I am trying to create a client script to display a system message based on the users language.

(This is a small part of what I am trying to do, but after failing for 20 hours; I am trying small bites.)

This is the code I am using:

function onLoad() {

var language = g_user.getLanguage();

    g_form.addInfoMessage(language);

}

I have also tried variations using

var loginLanguage = g_user.getClientData("loginlanguage");

With correct variables set,,,

And GlideAjax...

 

Thanks for your time,

1 ACCEPTED SOLUTION

as per my understanding it is acting like this way just because of we are dealing in different   scope. when you will try the same thing in global then it will work perfectly.


may i know your exact requirement?


View solution in original post

11 REPLIES 11

Dubz
Mega Sage

Hi Lon,



getLanguage() is not available on the client side, you'll need to write a script include to get the users language and then pass it to the client with a GlideAjax.



For a good video on how to write GlideAjax check out Ask the Expert: GlideAjax - TechNow Ep 33


Harsh Vardhan
Giga Patron

Hi Lon,



it will not work. because getLanguage() will execute in server side .



you can try with display business rule or glide ajax.



refer the link below


http://wiki.servicenow.com/index.php?title=Getting_a_User_Object#gsc.tab=0


Harsh Vardhan
Giga Patron

Please find the sample script below.



Display Business Rule.:



(function executeRule(current, previous /*null when async*/) {



// Add your code here


g_scratchpad.check= gs.getUser().getLanguage();



})(current, previous);



Client Script:



function onLoad() {


    //Type appropriate comment here, and begin script below


    alert(g_scratchpad.check);


}




I have created display business rule and stored value in g_scratchpad variable and used this variable in onLoad() client script.



http://wiki.servicenow.com/index.php?title=Scripting_in_Business_Rules#gsc.tab=0


Hi Harsh vardhan,



Thanks for the reply.




After following the instructions; the alert returns "undefined".