# QueryParam in SDK not working

**URL:** <https://community.cumulocity.com/t/queryparam-in-sdk-not-working/315>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [July 10, 2023, 6:04am UTC](https://community.cumulocity.com/t/queryparam-in-sdk-not-working/315 "2023-07-10T06:04:07Z")\
**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:** [July 10, 2023, 6:04am UTC](https://community.cumulocity.com/t/queryparam-in-sdk-not-working/315/1 "2023-07-10T06:04:07Z")

</div>

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

Cumulocity Production

#### _Detailed explanation of the problem:_

Hi,

I am using a custom query and I created a class as CustomQueryParam where I am fetching my devices based on creationTime. My service is a scheduled service and it is deployed on my tenant. First day when my service runs, it fetches the data based on current time only but next day when it runs it takes the previous time only, it doesn’t fetch the time from the customQueryParam.

My Code:

**CustomQueryParam Class**

```auto

public class CustomQueryParam extends QueryParam {

    private final Logger logger = Logger.getLogger(this.getClass().getName());

    private static final Param QUERY_PARAM = () -> "query";

    private static final CustomQueryParam INSTANCE = new CustomQueryParam();

    private CustomQueryParam() {
        super(QUERY_PARAM, URLEncoder.encode("creationTime.date ge " + "'" + CustomUtil.getDateWithTime() + "'", StandardCharsets.UTF_8));
        logger.info("Creation Time Query Param as: creationTime.date ge + '" + CustomUtil.getDateWithTime() + "'");
    }

    public static CustomQueryParam getInstance() {
        return INSTANCE;
    }
}

```

**CustomUtil Class**

```auto
public class CustomUtil {
    public static String getDateWithTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        final DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(cal.getTime()) +"T00:00:00.000Z";
    }
}

```

**MyService class method:**

```auto
public void getDevices() {
        List<Map<String, String>> deviceData = new ArrayList<>();
        subscriptionsService.runForEachTenant(() -> {

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

            Iterable<ManagedObjectRepresentation> mObjColl = inventoryApi.getManagedObjectsByFilter(deviceFilter)

                    .get(2000, WithParentsParameter.getInstance(), CustomQueryParam.getInstance()).allPages();

            logger.info("API Call: " + inventoryApi.getManagedObjectsByFilter(deviceFilter)
                    .get(2000, WithParentsParameter.getInstance(), CustomQueryParam.getInstance()).getSelf());

            List<ManagedObjectRepresentation> mObjList = StreamSupport.stream(mObjColl.spliterator(), false).toList();
            System.out.println();
            logger.info("No. of devices received: "+mObjList.size());
});
}

```

**Now in the deployed service logs:**  
**First time it ran since deployment**

```auto
2023-07-08 03:00:00.002 INFO 1 --- [scheduling-1] c.s.r.S.Schedule.Scheduler : Current Time: 2023-07-08T01:00:00.000Z
2023-07-08 03:00:00.224 INFO 1 --- [scheduling-1] c.s.r.S.utils.CustomQueryParam : Creation Time Query Param as: creationTime.date ge + '2023-07-07T00:00:00.000Z'
2023-07-08 03:00:01.054 INFO 1 --- [scheduling-1] c.s.r.S.Job.CumulocityService : API Call: {{url}}/inventory/managedObjects?query=creationTime.date%20ge%20'2023-07-07T00:00:00.000Z'&pageSize=2000&withParents=true&fragmentType=c8y_IsDevice&currentPage=1

```

**When it ran on 2nd day:**

```auto
2023-07-09 03:00:00.001 INFO 1 --- [scheduling-1] c.s.r.S.Schedule.Scheduler : Current Time: 2023-07-09T01:00:00.000Z
2023-07-09 03:00:02.931 INFO 1 --- [scheduling-1] c.s.r.S.Job.CumulocityService : API Call: http://t173516.iot-dev.solenis.com/inventory/managedObjects?query=creationTime.date%20ge%20'2023-07-07T00:00:00.000Z'&pageSize=2000&withParents=true&fragmentType=c8y_IsDevice&currentPage=1

```

Here you can see we didnt get that CustomQueryParam log it directly jumped to API Call log by assuming the first time since deployment in creationTime.getInstance method.

Similarly, today this happened as well so why is this happening? Do you know what could be the issue here 🤔

Hope you understood my question.

Thanks & Regards,  
Samanyu

---

<div class="post-metadata">

**Author:** ![Korbinian\_Butz](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/korbinian_butz/32/10473_2.png) [@Korbinian\_Butz](https://community.cumulocity.com/u/Korbinian_Butz)\
**Post date:** [July 10, 2023, 6:35am UTC](https://community.cumulocity.com/t/queryparam-in-sdk-not-working/315/2 "2023-07-10T06:35:17Z")

</div>

Your log output is defined within the constructor of `CustomQueryParam`. However, this class is implemented as singleton, so it will be instantiated only once. `CustomQueryParam.getInstance()` only returns the existing `INSTANCE` object and does not create a new one → constructor not executed, no log line to expect.

---

<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:** [July 10, 2023, 7:04am UTC](https://community.cumulocity.com/t/queryparam-in-sdk-not-working/315/3 "2023-07-10T07:04:02Z")

</div>

Okay that makes much more sense. Could you please let me know how I can configure my custom class to fetch the devices according to my use case. I need to fetch the devices based on creation Time so where should I place my creationTime.ge line

` super(QUERY_PARAM, URLEncoder.encode("creationTime.date ge " + "'" + CustomUtil.getDateWithTime() + "'", StandardCharsets.UTF_8));`

apart from constructor.

---

<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:** [July 10, 2023, 7:49am UTC](https://community.cumulocity.com/t/queryparam-in-sdk-not-working/315/4 "2023-07-10T07:49:27Z")

</div>

Hi Samanyu,

for this topic we discussed already in a different thread.  
May I know why this solution does not work for you for the “query” part?

You can use it like this:

```auto
QueryParam queryParam = CustomQueryParam.QUERY.setValue("creationTime.date ge " + "'" + CustomUtil.getDateWithTime() + "'").toQueryParam();

```

> [@How to use custom and other filters in Java sdk that are provided in REST Endpoints](https://community.cumulocity.com/t/how-to-use-custom-and-other-filters-in-java-sdk-that-are-provided-in-rest-endpoints/3909/2):
>
> Hi @Sam123 , for a time range you can use it as follows: Be aware of using “creationTime.date”, check [API documentation](https://cumulocity.com/api/10.15.0/#section/Supported-values). creationTime.date gt '2023-06-12T12:00:00.000Z' and creationTime.date le '2023-06-12T12:59:59.999Z See below and use “query” and just put the query above as value. In the “get” method you can set any query parameter like this The first argument can be the pageSize, here it is 50. import com.cumulocity.sdk.client.Filter; import com.cumulocity.sdk.client.QueryParam…

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:** [July 10, 2023, 12:40pm UTC](https://community.cumulocity.com/t/queryparam-in-sdk-not-working/315/5 "2023-07-10T12:40:57Z")

</div>

Hi Kai,

Yeah, but that time I went with the basic approach that I did thinking that it is right 😆 , but anyway thank you for your help 🙂. I have implemented what you said, just need to see if it works for 2 days straight.

---

<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:** [January 6, 2024, 12:41pm UTC](https://community.cumulocity.com/t/queryparam-in-sdk-not-working/315/6 "2024-01-06T12:41:13Z")

</div>

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