# How to get the next page data in JAVA SDK

**URL:** https://community.cumulocity.com/t/how-to-get-the-next-page-data-in-java-sdk/3150
**Category:** Forum
**Tags:** cumulocity
**Created:** [June 13, 2023, 10:57am UTC](https://community.cumulocity.com/t/how-to-get-the-next-page-data-in-java-sdk/3150 "2023-06-13T10:57:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Sam123](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/sam123/32/14460_2.png) [@Sam123](https://community.cumulocity.com/u/Sam123)
#### Post date: [June 13, 2023, 10:57am UTC](https://community.cumulocity.com/t/how-to-get-the-next-page-data-in-java-sdk/3150/1 "2023-06-13T10:57:58Z")

</div>

#### _ **Product/components** used and **version/fix** level:_

Cumulocity Production

#### _Detailed explanation of the problem:_

Hi, I am trying to fetch MO ids. I used InventoryFilter.byIds method and passed an id to get the data. Then I used getManagedObjectsByFilter method and passed the filter object and .get(2000) to fetch 2000 records. Then I used PagedManagedObjectCollectionRepresentation to fetch the ids of the devices.

```auto
InventoryFilter filter = new InventoryFilter().byIds(new GId(id));
PagedManagedObjectCollectionRepresentation p = inventoryApi.getManagedObjectsByFilter(filter).get(2000);

```

Now here as you can see I am getting only 2000 records, but we also get a next variable and with that we can check if there are more records. So I need to fetch those records but I don’t know how?

Also, can you please suggest me if this is a good way to fetch the data or if there is any other better way to fetch the data.

Thanks & Regards,  
Samanyu

---

<div class="post-metadata">

### Author: ![Kai\_Sieben](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/kai_sieben/32/5541_2.png) [@Kai\_Sieben](https://community.cumulocity.com/u/Kai_Sieben)
#### Post date: [June 13, 2023, 11:16am UTC](https://community.cumulocity.com/t/how-to-get-the-next-page-data-in-java-sdk/3150/2 "2023-06-13T11:16:34Z")

</div>

Hi Samanyu,

a convenient way is to use the `.allPages()` method which returns an Iterable.  
Within the `get(2000)` you can specify the page size - as you correctly did!

```auto
Iterable<ManagedObjectRepresentation> moList = inventoryApi.getManagedObjectsByFilter(filter).get(2000).allPages();
	
for (ManagedObjectRepresentation managedObjectRepresentation : moList) {
	// do stuff
}

```

I believe in your example you actually use a different filter correct? 🙂

Depending on the total number of objects there are better ways to do so in terms of performance.

Regards  
Kai

---

<div class="post-metadata">

### Author: ![Sam123](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/sam123/32/14460_2.png) [@Sam123](https://community.cumulocity.com/u/Sam123)
#### Post date: [June 13, 2023, 12:34pm UTC](https://community.cumulocity.com/t/how-to-get-the-next-page-data-in-java-sdk/3150/3 "2023-06-13T12:34:56Z")

</div>

Hi Kai,

I am actually using this filter only just without .allPages. Can you please tell me what better ways which can improve the performance?

Thanks & Regards,  
Samanyu

---

<div class="post-metadata">

### Author: ![Kai\_Sieben](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/kai_sieben/32/5541_2.png) [@Kai\_Sieben](https://community.cumulocity.com/u/Kai_Sieben)
#### Post date: [June 13, 2023, 12:44pm UTC](https://community.cumulocity.com/t/how-to-get-the-next-page-data-in-java-sdk/3150/4 "2023-06-13T12:44:35Z")

</div>

Hi Samanyu,

filtering on a specific ID will always return at most 1 object. So there is no need to page the results, that is why I am asking.  
The optimized way only works in case you are targeting to get all objects which match a given filter-query and do not require sorting.

Could you please elaborate what kind of objects and how many you will try to collect?  
Then only I will be able to show the optimized way.

Thanks!

---

<div class="post-metadata">

### Author: ![Sam123](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/sam123/32/14460_2.png) [@Sam123](https://community.cumulocity.com/u/Sam123)
#### Post date: [June 13, 2023, 1:08pm UTC](https://community.cumulocity.com/t/how-to-get-the-next-page-data-in-java-sdk/3150/5 "2023-06-13T13:08:00Z")

</div>

Hi Kai,

I have multiple use cases.

1. So, one of the cases is related to MO, my devices are organized in a such a way like first there is a group then there is a sub group and in that subgroup there will be multiple devices. So how I am fetching the devices or the subgroups are I am directly fetching the devices through a filter Like :-

```auto
 InventoryFilter filter = new InventoryFilter().byIds(new GId(id));

PagedManagedObjectCollectionRepresentation p = inventoryApi.getManagedObjectsByFilter(filter).get(2000);

p.getManagedObjects().get(i).getChildAssets().getReferences().get(j).getManagedObject().getId().getValue()

```

So with this I get the subgroups, now i will run again the same code with the subgroup Ids and I will get the respective devices.

1. The second use case is related to getting the measurements. So in this what I am fetching all the measurements. Now the measurements could be more than 2000, so as you suggested I will be using the .allPages().

```auto
MeasurementFilter measurementFilter = new MeasurementFilter().bySource(new GId("60464")).byType("type").byDate("", CommonUtil.currentTime());

PagedMeasurementCollectionRepresentation p = measurementApi.getMeasurementsByFilter(measurementFilter).get(1, RevertQueryParameter.getInstance());

```

1. So for third case, i want to fetch all the devices based on a type. So what I am doing is :-

```auto
InventoryFilter deviceFilter = new InventoryFilter().byFragmentType("c8y_IsDevice");

PagedManagedObjectCollectionRepresentation p = inventoryApi.getManagedObjectsByFilter(deviceFilter).get(2000);
p.getManagedObjects().get(i).getId().getValue();

```

This way I will directly get the id of the MOs.

These are the use cases that I have, can you suggest optimization for all.

Thanks & Regards,  
Samanyu

---

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)
#### Post date: [December 10, 2023, 1:08pm UTC](https://community.cumulocity.com/t/how-to-get-the-next-page-data-in-java-sdk/3150/6 "2023-12-10T13:08:23Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
