# Okta Java SDK Client.listUsers() only returning first 200 users

**URL:** <https://devforum.okta.com/t/okta-java-sdk-client-listusers-only-returning-first-200-users/21398>\
**Category:** Questions\
**Tags:** java\
**Created:** [July 27, 2022, 9:03pm UTC](https://devforum.okta.com/t/okta-java-sdk-client-listusers-only-returning-first-200-users/21398 "2022-07-27T21:03:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tjpspf](https://avatars.discourse-cdn.com/v4/letter/t/838e76/32.png) [@tjpspf](https://devforum.okta.com/u/tjpspf)\
**Post date:** [July 27, 2022, 9:03pm UTC](https://devforum.okta.com/t/okta-java-sdk-client-listusers-only-returning-first-200-users/21398/1 "2022-07-27T21:03:12Z")

</div>

The following code is only returnning the first 200 users when I attempt to either loop or stream through the UserList that is returned from Client.listUsers(). Am I doing something wrong? I though pagination would be handled via the Okta Java SDK.

```auto
@Override
    public Client getClient(String oktaDomain, String apiToken) {
        String oktaUrl = "https://" + oktaDomain;

        return Clients.builder()
                .setRetryMaxElapsed(REQUEST_TIMEOUT)
                .setRetryMaxAttempts(MAX_RETRIES)
                .setOrgUrl(oktaUrl)
                .setClientCredentials(new TokenClientCredentials(apiToken))
                .setScopes(new HashSet<>(Collections.singletonList("okta.users.read")))
                .build();
    }

    @Override
    public UserList getUserData(String domain, String apiToken) throws CustomAllowRetryException {
        Client client = getClient(domain, apiToken);
        UserList userList;
        try {
            logger.info("Gathering user list from Okta.", v("domain", domain));
            userList = client.listUsers();
        } catch (RuntimeException runtimeException) {
            logger.error("Unable to retrieve Users from Okta account for domain.", v("domain", domain), v("message", runtimeException.getMessage()));
            throw new WombatAllowRetryException("Unable to retrieve Users from Okta account for domain");
        } catch (Exception exception) {
            logger.error("Unable to retrieve Users from Okta account for domain.", v("domain", domain), v("message", exception.getMessage()));
            throw new CustomAllowRetryException("Unable to retrieve Users from Okta account for domain");
        }

        return userList;
    }

```

---

<div class="post-metadata">

**Author:** ![bdemers](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/bdemers/32/23_2.png) [@bdemers](https://devforum.okta.com/u/bdemers)\
**Post date:** [July 28, 2022, 2:43pm UTC](https://devforum.okta.com/t/okta-java-sdk-client-listusers-only-returning-first-200-users/21398/2 "2022-07-28T14:43:03Z")

</div>

Can you include the snippet you are using to loop through the UserList?

---

<div class="post-metadata">

**Author:** ![tjpspf](https://avatars.discourse-cdn.com/v4/letter/t/838e76/32.png) [@tjpspf](https://devforum.okta.com/u/tjpspf)\
**Post date:** [July 28, 2022, 5:41pm UTC](https://devforum.okta.com/t/okta-java-sdk-client-listusers-only-returning-first-200-users/21398/3 "2022-07-28T17:41:14Z")

</div>

Yes, @bdemers, I’m running into issues while looping throught the list. One challenge is that I need to be able to retrieve each user’s manager’s email address and map it to that user inside my `grpcService`.

The following code starts with a `UserList oktaUserList` that comes from calling `Client.listUsers()`.

```auto
// Create local cache of userIds and userEmails to use for mapping managerEmail
Map<String, String> oktaUserIdEmailCache = new HashMap<>();

// NOTE: This forEach() works successfully for 15,000 users
oktaUserList.forEach(user -> {
    oktaUserIdEmailCache.put(user.getId(), user.getProfile().getEmail());
});

int oktaUserListSize = oktaUserIdEmailCache.size();
int oktaUsersRemaining = oktaUserListSize;

List<com.okta.sdk.resource.user.User> oktaUserBatch = new ArrayList<>();
int oktaUserBatchNumber = 0;

// NOTE: This for loop is NOT working; Only goes through first 200 users
for (com.okta.sdk.resource.user.User user : oktaUserList) {
    if (oktaUserBatch.size() < BATCH_SIZE) {
        oktaUserBatch.add(user);
        oktaUsersRemaining--;
    }

    if (oktaUserBatch.size() == BATCH_SIZE || oktaUsersRemaining == 0) {
        if (oktaUsersRemaining == 0) {
            isFinished = true;
        }
        oktaUserBatchNumber++;

        // NOTE: This is where the managerEmail mapping takes place
        List<grcpcUser> grpcUsers = grpcService.convertOktaUserListToGrpcUserList(oktaUserBatch, oktaUserIdEmailCache);

        UserResponse response = grpcService.sendUsersToGrpcProcessor(grpcUsers, syncTypeName, id, correlationID, tenantUUID,
                adminEmail, isFinished, batchNumber, totalUserCount);

        if (oktaUsersRemaining > 0) {
            oktaUserBatch.clear();
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![bdemers](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/bdemers/32/23_2.png) [@bdemers](https://devforum.okta.com/u/bdemers)\
**Post date:** [July 28, 2022, 7:57pm UTC](https://devforum.okta.com/t/okta-java-sdk-client-listusers-only-returning-first-200-users/21398/4 "2022-07-28T19:57:16Z")

</div>

On the second loop, are the user objects part of the last page?

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [February 12, 2024, 9:26pm UTC](https://devforum.okta.com/t/okta-java-sdk-client-listusers-only-returning-first-200-users/21398/5 "2024-02-12T21:26:16Z")

</div>


