Get all the values in an array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2024 07:47 AM
HI All,
I have an requirement where I am using for loop to get the values from multi row variable set. Based on the no.of disks selected I am sending the values to 3rd party through an API. It is working for 2 disks but when I select 20 disks I am not able to get all the disk values. Here is my script.
I am only getting 7 values from array which is from 33 to 39
Please help me to get all the 20 rows values in the array.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2024 10:13 AM
I don't see what would be preventing the remaining rows in this case, but unless there's the possibility that the MRVS will contain more rows than the number supplied in the variable, you can just use this approach to retrieve the full MRVS then loop through the rows
var mrvs = current.variables.disk_label_and_size_gb;
var rowCount = mrvs.getRowCount();
for (var i=0; i<rowCount; i++) {
var row = mrvs.getRow(i);
if (i==0) {
r.setStringParameterNoEscape('DiskLabel1', row.disk_label1);
r.setStringParameterNoEscape('DiskSize1', row.disk_size1);
} else if (i==1) {
...
It looks similar to your approach, so maybe there's something in the MRVS JSON that is causing it to stop, or you're running into a character limitation in the parameters that you can set? You could add logs to the remaining if blocks to see if they are being executed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 05:15 AM
hi @Brad Bowman ,
Thank for the reply. the code worked for 20 disks but now it is not allowing me to trigger API for lesss than 20.
Please help me with a condition where it should work if we select 1,2 ,3 ...... till 20.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 05:42 AM
That's not making sense to me. This code just gets the contents of the MRVS then runs once for each row of data, so it's dynamic - the loop won't run at all for an empty MRVS, it will run once when there's one row... are you perhaps saying that you are always populating 20 rows in the MRVS, but sometimes only want to trigger the API for less than 20 rows?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 05:33 AM
Hi @Phanideepthi ,
Try using below code.
var mrvs = current.variables.disk_label_and_size_gb;
var rowCount = mrvs.getRowCount();
for (var i=1; i<=rowCount; i++) {
var row = mrvs.getRow(i);
r.setStringParameterNoEscape('DiskLabel'+i, row.disk_label1);
r.setStringParameterNoEscape('DiskSize'+i, row.disk_size1);
}
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------