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.

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

Community Alums
Not applicable

in your HTML script please try

{{c.data.server}}

instead of

data.server

 

In your server script it should be 

data.server

Micael Marinho1
Giga Contributor

Hi @Aidan Wardman the problem persists, the log on the browser console shows the following:

c is not defined at eval

Community Alums
Not applicable

Can you please make sure your widget has the following code wrappers (which are there when you first create it).

find_real_file.png

You should also have this

find_real_file.png

 

If you are missing any of these things, then that will be causing your issue.

Micael Marinho1
Giga Contributor

@Aidan Wardman it's exactly like the widget's default when I created it

Omkar Mone
Mega Sage

Hi Micael,

 

Can you try with -

console.log("test: " + top.VAL);

 

I tried with it, seems to work. Check it once from your end and let me know.

 

Regards

Omkar Mone