The CreatorCon Call for Content is officially open! Get started here.

How to use the service portal "data" object in a HTML <script> tag

Micael Marinho1
Giga Contributor

Hi guys

I am implementing the google maps API on the portal, however to call it, it is necessary to place the code inside a <script> tag in HTML instead of using the common client script. The problem is that I need to pull information from tables in the servicenow to put on the map, so ideally I would be able to access the data object, from the <script> tag. I tried to use UI script with GlideAjax and the client's GlideRecord, but this type of call just doesn't work from the service portal.

Can someone help me?

I thank you for your attention, have a good day !!!

1 ACCEPTED SOLUTION

Micael Marinho1
Giga Contributor

Hi guys

I was able to solve the problem with the following code:

 

HTML:

<div>
  <script>
    setTimeout(function(){
    	console.log("community: " + top.val);
    }, 10);
  </script>
</div>

Client Controller:

function($scope, spUtil) {
  /* widget controller */
  var c = this;
  top.val = $scope.data.server;
}

Server Script:

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
  data.server = "test";
})();

The explanation is that when we set the value of "top" in the client controller there is no time for the <script> tag to load it. Then we use the setTimeout() javascript function in the <script> tag to give it a little time.

 

Regards,

Micael Marinho

View solution in original post

16 REPLIES 16

Omkar Mone
Mega Sage

Hi 

Just updating the code, in HTML under the script tag use the top object,where you can set the value from the server  - 

HTML - 

<div>
  <script>
    console.log(top.Val);
  </script>
</div>

 

Client Controller - 

function($scope) {
  /* widget controller */
  var c = this;	
top.Val = c.data.valuefromserver;
top.okok = c.data.test111;
	
	
}

 

Server Script - 

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */

	data.valuefromserver = "test";
        data.test111 = "ok";
	
})();

 

Hope this helps.

 

Regards

Omkar Mone

2020 Community MVP

 

Hi @Omkar Mukesh Mone 

 

before when I used console.log() pulling a variable from another script, the google maps map didn't even load, now it loads. But when I put the following code:

<div>
  <script>
  	console.log("community: " + top.val);
  </script>
</div>
function($scope, spUtil) {
  /* widget controller */
  var c = this;
	top.val = $scope.data.server;
}
(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
	data.server = "test";
})();

The browser console returns this message to me:

"community: undefined"

Upparapalli
Tera Contributor

Hi,

You can try like this 

console.log("test: " + c.data.server); 

                   or

console.log("test: " + $scope.data.server); 

 

Regards,

Arjun

Hi @Arjun 

 

The browser return this to me:

 

"c is not defined at eval" and

"$scope is not defined at eval"

Micael Marinho1
Giga Contributor

Hi guys

I was able to solve the problem with the following code:

 

HTML:

<div>
  <script>
    setTimeout(function(){
    	console.log("community: " + top.val);
    }, 10);
  </script>
</div>

Client Controller:

function($scope, spUtil) {
  /* widget controller */
  var c = this;
  top.val = $scope.data.server;
}

Server Script:

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
  data.server = "test";
})();

The explanation is that when we set the value of "top" in the client controller there is no time for the <script> tag to load it. Then we use the setTimeout() javascript function in the <script> tag to give it a little time.

 

Regards,

Micael Marinho