Help getting TicketID that is displayed to users

I will start by saying I’m outside of my depth here - I’m not a JS guy at all. I’m a C# guy - so this could easily be me, but I don’t think it is.

I am trying to get the Ticket ID that is displayed to the user. Eg, if they are working on ticket #1234, I want to receive 1234. Then, I’m trying to pad it to a total of 10 digits, so there will be some leading 0s to get it there (at least for now). However, I’m getting some random number. According to the documentation, I believe I should be getting the display_id instead, but when I call that, I simply get an empty value.

app.js

document.addEventListener('DOMContentLoaded', function() {
  app.initialized().then(function(_client) {
    window.client = _client;
    _client.events.on('app.activated', function() {
      _client.data.get('ticket').then(function(data) {
        var ticketId = data.ticket.id;
        var formattedTicketId;

        if (ticketId.length < 10) {
          formattedTicketId = ticketId.padStart(10, '0');
        } else if (ticketId.length > 10) {
          formattedTicketId = ticketId.substring(0, 10);
        } else {
          formattedTicketId = ticketId;
        }

        document.getElementById('ticketId').innerText = formattedTicketId;
      }, function(error) {
        console.error('Error fetching ticket data', error);
      });
    });
  }, function(error) {
    console.error('Error initializing app', error);
  });
});

index.html

<html>
  <head>
    <script src="{{{appclient}}}"></script>
    <link rel="stylesheet" type="text/css" href="styles/style.css" />
  </head>

  <body>
    <section class="app-body">
      <h4>Generated Control Fields:</h4>
      <div id="ticketId" style="font-size: 14px; font-weight: bold;"></div>
      <p id="apptext"></p>
    </section>
  </body>
  <script src="scripts/app.js"></script>
</html>

In the attached screenshot, I’m looking for the value to be 107118, but I’m receiving some other value.

I have also tried display_id instead of just id, but that yielded no results.

Hi @mbutler756

I noticed that the display_id isn’t mentioned in the ticket response.

Doc : Ticket Data Method

Could you please log and verify the data.ticket.id? Also, note that the id is of type integer.

Hi there,

you are mixing F-Service and F-Desk @vangakirankumarreddy.
The request is on Freshservice, you are on Desk.

@mbutler756, did you log the whole data.ticket object?
If not, you may find the number you are looking for in there.

I am not into Freshservice. I tried to understand the API documentation, but I was not able to figure out, which field you need.
So maybe you could log the whole data.ticket object and look for the number???

Another option would be to just use the browser for getting the ticket data.
If you call https://yourdomain.freshservice.com/api/v2/tickets/[id], you should get the JSON and search for the [id].
Works in Freshdesk, hopefully in Service too.

Hope that helps!

Best
Tom

Hi @mbutler756

The data.ticket.display_id field exists in Freshservice under the ticket data API.

Freshservice Ticket Data API Documentation

Thank you, @ThomasH, for the correction.

1 Like

Thanks Vanga. I have tried both ‘’‘id’‘’ (what I’m currently using) and ‘’‘display_id’‘’

Oddly enough…I just used ‘’‘display_id’‘’ again and it’s working – I guess I was doing something wrong the first time, but the value is displaying properly!!! Thank you.

I modified the code just slightly to pad ‘’‘TKT#’‘’ to the front when the length of the ‘’‘ticketId’‘’ variable is < 10 chars. That’s not displaying at the moment, but I should be able to troubleshoot that (unless you see something glaringly obvious).

‘’’
document.addEventListener(‘DOMContentLoaded’, function() {
app.initialized().then(function(_client) {
window.client = _client;
_client.events.on(‘app.activated’, function() {
_client.data.get(‘ticket’).then(function(data) {
var ticketId = data.ticket.display_id;
var formattedTicketId;

    if (ticketId.length < 10) {
      formattedTicketId = ticketId.padStart(10, 'TKT#');
    } else if (ticketId.length > 10) {
      formattedTicketId = ticketId.substring(0, 10);
    } else {
      formattedTicketId = ticketId;
    }

    document.getElementById('ticketId').innerText = formattedTicketId;
  }, function(error) {
    console.error('Error fetching ticket data', error);
  });
});

}, function(error) {
console.error(‘Error initializing app’, error);
});
});
‘’’

Just a quick note - this was my first post here and my first foray into JS. Thank you so much for the assistance you have given me - such a great community of devs. Thank you for the assistance and kindness you have shown. JS is not my forte

I was able to resolve this. It turns out that the length property is only available to a string. Once converting my variable to a string - everything worked as expected.

Thank you for your help and assistance! I truly appreciate the community and the advise you have shared with me!

1 Like