Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Thanks


Hi Anurag,



If the answer has resolved your issue, could you please mark the answer as correct, so it will help future users of community



Thanks,