@Override
  public List<EntityRecord> getEntityRecordsFromLookup(
      Long entityId, String lookupName, Map<String, Object> lookupMap, QueryParams queryParams) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);

    LookupDto lookup = getLookupByName(entityId, lookupName);
    List<FieldDto> fields = entityService.getEntityFields(entityId);
    Map<String, FieldDto> fieldMap = entityService.getLookupFieldsMapping(entityId, lookupName);

    MotechDataService service = getServiceForEntity(entity);

    try {
      LookupExecutor lookupExecutor = new LookupExecutor(service, lookup, fieldMap);

      Object result = lookupExecutor.execute(lookupMap, queryParams);

      if (lookup.isSingleObjectReturn()) {
        EntityRecord record = instanceToRecord(result, entity, fields, service);
        return (record == null) ? new ArrayList<EntityRecord>() : Collections.singletonList(record);
      } else {
        List instances = (List) result;
        return instancesToRecords(instances, entity, fields, service);
      }
    } catch (RuntimeException e) {
      throw new LookupExecutionException(e);
    }
  }
  @Override
  public long countRecordsByLookup(
      Long entityId, String lookupName, Map<String, Object> lookupMap) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);

    LookupDto lookup = getLookupByName(entityId, lookupName);
    Map<String, FieldDto> fieldMap = entityService.getLookupFieldsMapping(entityId, lookupName);

    MotechDataService service = getServiceForEntity(entity);

    try {
      LookupExecutor lookupExecutor = new LookupExecutor(service, lookup, fieldMap);
      return lookupExecutor.executeCount(lookupMap);
    } catch (RuntimeException e) {
      throw new LookupExecutionException(e);
    }
  }