how to get data from client script to html.

Hm10
Tera Contributor

This is HTML code,

  <td class="ticket-symbol">✉️</td>
            <td>Raised tickets</td>
           <td class="circle-container">
<div class="circle">{{c.data.loggedInUser}}</div>
</td>
<td class="circle-container" id="raisedTicketCount">
    <div class="circle" ng-bind="c.data.requestItemCount"></div>
</td>

This is Client side. 

 console.log("Client-side script is running...");
alert("Client-side script is running...");

    c.server.get().then(function(response) {
        console.log("Data fetched from server:", response.data);


        c.data.loggedInUser = response.data.loggedInUser;
        c.data.requestItemCount = response.data.requestItemCount;
    });

This is server side script

(function() {
    var userName = gs.getUser().getFullName(); 
    data.loggedInUser = userName;

    var requestItemCount = 0;
    var requestItemGr = new GlideRecord('sc_req_item'); // Using 'sc_req_item' table for request items

    // Add an encoded query to fetch items requested by the current logged-in user
    requestItemGr.addEncodedQuery("requested_for.name=" + userName);
    requestItemGr.query();

    while (requestItemGr.next()) {
        requestItemCount++;
    }

    // Send the data to the client-side script
    data.requestItemCount = requestItemCount;

    // Log for debugging
  gs.info("Sending data to client: loggedInUser=" + data.loggedInUser + ", requestItemCount=" + data.requestItemCount);

	gs.info("Request Item Count: " + requestItemCount);
    gs.info("Current User's Username: " + userName);
})();

 

I want to display User full name and count of all tickets raised by logged in user. My logs are showing correct values but cannot fetch them in html and are not visible in the page. What am I doing wrong ?

5 REPLIES 5

Peter Bodelier
Giga Sage

Hi @Hm10,

 

Are you building this in global or in a scoped app? In Global it is working fine for me.

 

In scoped I needed to change 

    var userName = gs.getUser().getFullName(); 

 

to

var userName = gs.getUser().getDisplayName(); 

 

Apart from that, please use a GlideAggregate Query instead of looping through the query result to get the amount of records.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

sanket16
Giga Guru

Hi @Hm10 ,

If you have no operation to do in client script,then you can directly pass the data from server to HTML.

In HTML:

<div class="circle">{{data.loggedInUser}}</div>
<div class="circle">{{data.requestItemCount}}</div>

 

Mohith Devatte
Tera Sage
Tera Sage

Hello @Hm10 

No need to use client script for this 

you can directly access it via server side script in HTML like below 

<td class="ticket-symbol">✉️</td>
            <td>Raised tickets</td>
           <td class="circle-container">
<div class="circle">{{data.loggedInUser}}</div>
</td>
<td class="circle-container" id="raisedTicketCount">
    <div class="circle" ng-bind={{data.requestItemCount}}></div>
</td>

 

Hope this helps 

Mark the answer correct if this helps you 

Thanks

Hi @Mohith Devatte 

This in not displaying for me