@Override @Transactional public <T> Records<T> getRelatedFieldValue( Long entityId, Long instanceId, String fieldName, QueryParams queryParams) { EntityDto entity = getEntity(entityId); validateCredentials(entity); String entityName = entity.getName(); MotechDataService service = getServiceForEntity(entity); Object instance = service.findById(instanceId); if (instance == null) { throw new ObjectNotFoundException(entityName, instanceId); } try { Collection<T> relatedAsColl = TypeHelper.asCollection(PropertyUtil.getProperty(instance, fieldName)); List<T> filtered = InMemoryQueryFilter.filter(relatedAsColl, queryParams); int recordCount = relatedAsColl.size(); int rowCount = (int) Math.ceil(recordCount / (double) queryParams.getPageSize()); return new Records<>(queryParams.getPage(), rowCount, recordCount, filtered); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new ObjectReadException(entityName, e); } }