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 get last sys id value in the array

AnilM99
Tera Expert

Hi team,

I need help in below script:

var responseBody = [{"title":"Videos","sysid":"2eiej589eady9e6aesblo52"},{"title":"Sales","sysid":"25ic5ej589eady9e6aesblo87oed3"},{"title":"Enablement","sysid":"d9e3seady9e6aesblo525eplse36"}];

I want last 'sysid' of responseBody

this is only example i have 500 sysids in responseBody

 

Thanks

anil

1 ACCEPTED SOLUTION

Amit Pandey
Kilo Sage
var responseBody = [{"title":"Videos","sysid":"2eiej589eady9e6aesblo52"},{"title":"Sales","sysid":"25ic5ej589eady9e6aesblo87oed3"},{"title":"Enablement","sysid":"d9e3seady9e6aesblo525eplse36"}];

var lastItem = responseBody[responseBody.length - 1];

var lastSysId = lastItem.sysid;

gs.info("Last 'sysid' of responseBody:" +lastSysId);

 

AmitPandey_0-1712665729828.png

 

Please mark my answer helpful and correct.

 

Regards,
Amit

 

View solution in original post

7 REPLIES 7

Hi @Amit

The script is working fine in background script but script include returns undefined value

 

Thanks

Hi @AnilM99 

 

Can you share your script include to debug?

 

Regards,

Amit

Maddysunil
Kilo Sage

@AnilM99 

You can try below

 

var responseBody = [
    {"title":"Videos","sysid":"2eiej589eady9e6aesblo52"},
    {"title":"Sales","sysid":"25ic5ej589eady9e6aesblo87oed3"},
    {"title":"Enablement","sysid":"d9e3seady9e6aesblo525eplse36"}
];

// Get the last element of the responseBody array
var lastItem = responseBody[responseBody.length - 1];

// Retrieve the 'sysid' property of the last item
var lastSysId = lastItem.sysid;

// Output the last sysid
gs.info("Last sysid:", lastSysId);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks