Fetching logged in user company, Fetching logged in user company

a2gupta
Giga Contributor

Hi,

I have a requirement to use the logged in user's company to validate some other fields. Now as far as I know,fetching the company name directly using User object is not possible. However we can get the company sys_id by using gs.getUser().getCompanyID. Now if i want to fetch the company name by query the company table with the company sys_id,what should be the code I have to use? I want to search the company table for the company name(comparing the auto poulated company sys_id i get into the sys_id field for the logged in user) for the logged in user and use the company name to validate some other fields. Like- if the user belongs to a certain company he should see some certain fields etc.

P.S- Very bad in coding,so a little explaining with any helpful answer will be really appreciated

Thanks,

1 ACCEPTED SOLUTION

ghsrikanth
Tera Guru

Please find the following GlideAjax script -



Client script - onLoad


function onLoad() {



      var ga = new GlideAjax('companyUtil');



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


      ga.getXML(analyzeResponse); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)


}



function analyzeResponse(response) {


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


      alert(answer);


      //you can do anything here with the form, the answer variable holds the name of the company


}




Script include -


  • When creating the script include, you must select the Client callable check box.

FYI, GlideAjax - ServiceNow Wiki



var companyUtil = Class.create();



companyUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {



      getCompanyName: function() {



              var compName = ''; // Return value


              var grComp   = new GlideRecord('core_company');



              if (grComp.get(gs.getUser().getCompanyID())) {


              compName = grComp.name;


              }


            return compName;


      }


});






Hopefully this should help


View solution in original post

6 REPLIES 6

ghsrikanth
Tera Guru

You have to write a GlideAjax call in onLoad script - this should help you and it has sample scripts also -


Client Script Best Practices - ServiceNow Wiki



Hopefully it helps


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Anurag,



Assuming you wanna do the validation at client side then you have to make a GlideAjax call.


The GlideAjax class allows the execution of server-side code from the client.



Please go through the below example for reference and adjust your code. Let me know if you are blocked.


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


Hi Pradeep,



If you can share what the code should be it will be really helpful


ghsrikanth
Tera Guru

Please find the following GlideAjax script -



Client script - onLoad


function onLoad() {



      var ga = new GlideAjax('companyUtil');



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


      ga.getXML(analyzeResponse); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)


}



function analyzeResponse(response) {


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


      alert(answer);


      //you can do anything here with the form, the answer variable holds the name of the company


}




Script include -


  • When creating the script include, you must select the Client callable check box.

FYI, GlideAjax - ServiceNow Wiki



var companyUtil = Class.create();



companyUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {



      getCompanyName: function() {



              var compName = ''; // Return value


              var grComp   = new GlideRecord('core_company');



              if (grComp.get(gs.getUser().getCompanyID())) {


              compName = grComp.name;


              }


            return compName;


      }


});






Hopefully this should help