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

Thanks Surya, if was the initial userCompany function call that was causing issues. I stuck with getXMLAnswer though as it negates the need to parse out the information using the response.responseXML.documentElement etc etc syntax.



Final client script:



var ga = new GlideAjax('GetUserCompany');


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


        ga.getXMLAnswer(returnComp);



function returnComp(response){


        if(response == 'e3e0c843db6c7200e576b14ffe9619dc'){


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


        }


}


Hello Dave,



If i answered your question, kindly mark my answer as correct


Dave Smith1
ServiceNow Employee
ServiceNow Employee

David Dubuis wrote:



Hi All,



I'm trying to write a client script...


.. does it need to be a client script?


So, the eventual goal is to have different views set up to partition off various different companies within the group. Once these views are in place the choice list options will be defined on that basis.



This is basically just a bit of a workaround until our professional services guys have done the business with the views. It's also a handy excuse to start getting to grips with GlideAjax records.


Dave Smith1
ServiceNow Employee
ServiceNow Employee

David Dubuis wrote:


This is basically just a bit of a workaround until our professional services guys have done the business with the views. It's also a handy excuse to start getting to grips with GlideAjax records.


That is definitely is!



Yeah, if you've got a "proper" way and this is just a temporary filler, I'd say go for it.