Why g_user API not available in a UI script associated to a Angular UI page

Rakesh Goel
Kilo Expert

I am new to ServiceNow. I was testing a developer training lab exercise regarding Angular Page UI. I get error using g_user.username. If I remove this reference everything else works fine. The error is also given below:

My Angular Client UI Script (x_304789_angular_a.angular_app_client_lo) is:

angular.module('angularapp',[]);
angular.module('angularapp').controller('AngularAppCtrl',function($scope){
 
 $scope.myVar = '';
 $scope.i = 0;
 $scope.clearTxt = function(){
  $scope.myVar = '';
  $scope.i++;
  alert("The myVar value was cleared " + $scope.i + " times by " + g_user.userName + ".");
 };
});

My Angular UI Page defined in Application Studio is:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"></link>
 <script src="x_304789_angular_a.angular_app_client_lo.jsdbx"></script>
 <div ng-app="angularapp" ng-controller="AngularAppCtrl as appctrl">
  <input type="text" ng-model="myVar" placeholder="Enter some text here"></input>
  <p></p>
  <div class="jumbotron">
   <h1 class="display-3">Hello World!</h1>
   <hr class="m-y-2" />
   <p>{{myVar}}</p>
  </div>
  <button ng-click="clearTxt()" type="btn btn-primary">Clear Text</button>
 </div>
</j:jelly>

I am getting this console error on pressing Clear Text button as it calls clearTxt() which in turn references g_user.userName. Please let me know why this object is not available in UI script:

  1. 'g_user' is not defined at $scope.clearTxt (https://dev68393.service-now.com/x_304789_angular_a.angular_app_client_lo.jsdbx:9:3) at fn (Function code:2:138) at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:284:187) at m.prototype.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:148:336) at m.prototype.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:149:40) at Anonymous function (https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:284:237) at hg (https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:39:297) at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:39:211)
 
 
 
 
 
 
 
 
 
 
No Results
 
1 ACCEPTED SOLUTION

Rakesh Goel
Kilo Expert

Thanks Paul. Actually, the issue was that on the UI page (HTML) piece, I was using the Direct option. This was making g_user unavailable. Once I unticked the Direct option, it worked like a charm.

View solution in original post

2 REPLIES 2

The SN Nerd
Giga Sage
Giga Sage

UI Scripts don't have access to global variables when used in Service Portal.
You'll need to pass it in somehow.

Maybe try this instead

$scope.page.g_user

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Rakesh Goel
Kilo Expert

Thanks Paul. Actually, the issue was that on the UI page (HTML) piece, I was using the Direct option. This was making g_user unavailable. Once I unticked the Direct option, it worked like a charm.