getting current user info on a client script

Dubz
Mega Sage

Hi All,

I'm trying to write a client script that will show a particular set of choice list options depending on the company the current user belongs to. The users company doesn't seem to be available via g_user so do i'm thinking i have to use a Glide Ajax? if that's correct can anyone advise on how to write this? I just watched technow epsiode33 on this and, along with other sources i've come up with the below but it doesn't seem to be working, a lot of it is guesswork so i have no clue where i've gone wrong

Client Script:

function userCompany(company){

var ga = new GlideAjax('GetUserCompany');

ga.addParam('sysparm_name','getComp');

ga.addParam('sysparm_company',company);

ga.getXMLanswer();

alert(ga.getAnswer());

}

if(userCompany == 'Company Name Here'){

g_form.clearOptions/addOption..blah blah blah....

Script Include:

var GetUserCompany = Class.create();

GetUserCompany.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getComp: function() {

var comp = this.getParameter("sysparm_company");

return gs.getUser().getCompanyID(company);

}

});

1 ACCEPTED SOLUTION

lSurya 24
Giga Guru

Hello David,




Client Script:


var ga = new GlideAjax('GetUserCompany');


ga.addParam('sysparm_name','getComp');


ga.getXML(HelloWorldParse);



function HelloWorldParse(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");   // answer will now have company sys id; if you want name of company. please do glide record company table to fetch the name of the company.


if(userCompany == 'Company sys id Here'){


g_form.clearOptions/addOption..blah blah blah....



   


}



Script Include:


var GetUserCompany = Class.create();


GetUserCompany.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getComp: function() {


return gs.getUser().getCompanyID(); //returns sys id of an company to client script as an answer


}


});


View solution in original post

9 REPLIES 9

Anurag Tripathi
Mega Patron
Mega Patron

Hi David,



Replace your client side code like this



function userCompany(company){


var ga = new GlideAjax('GetUserCompany');


ga.addParam('sysparm_name','getComp');


ga.addParam('sysparm_company',company);


ga.getXML(asyncGA);



}



function asyncGA(response)


{


var answer = response.responseXML.documentElement.getAttribute("answer");


if(answer== 'Company Name Here'){


g_form.clearOptions/addOption..blah blah blah....


}


-Anurag

Sharique Azim
Mega Sage

Hi David,



You can use g_form.getReference(); on the field, if its a reference field..




something like,



var comp= g_form.getReference('caller_id',test);   //suppose i build my client script on the incident table



if(comp== 'sys_id in string'){


//code


}




function test(ans){



return ans.company;


}



Read   http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#getReference   for info..


Thanks Sharique but i need to query the company of the current user logged on and viewing the form so there's no reference field.


lSurya 24
Giga Guru

Hello David,




Client Script:


var ga = new GlideAjax('GetUserCompany');


ga.addParam('sysparm_name','getComp');


ga.getXML(HelloWorldParse);



function HelloWorldParse(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");   // answer will now have company sys id; if you want name of company. please do glide record company table to fetch the name of the company.


if(userCompany == 'Company sys id Here'){


g_form.clearOptions/addOption..blah blah blah....



   


}



Script Include:


var GetUserCompany = Class.create();


GetUserCompany.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getComp: function() {


return gs.getUser().getCompanyID(); //returns sys id of an company to client script as an answer


}


});