getDocCount method

  1. @override
Future<RequestResponse<int?>> getDocCount({
  1. required String doctype,
  2. dynamic filters,
})
override

Returns the number of documents of a doctype available in the backend within RequestResponse.

Optionally can include filters to return the count based on a certain criteria.

Throws InvalidFrappeFilter if the filter does not conform to Frappé filters.

If the doctype does not exist, returns a failed RequestResponse.

Implementation

@override
Future<RequestResponse<int?>> getDocCount(
    {required String doctype, dynamic filters}) async {
  if (filters != null) {
    if (!DBFilter.isDBFilter(filters)) throw InvalidFrappeFilter();
  }
  final response = await Request.initiateRequest(
      url: '${config.hostUrl}',
      method: HttpMethod.POST,
      contentType: ContentTypeLiterals.APPLICATION_JSON,
      data: <String, dynamic>{
        'cmd': 'frappe.desk.reportview.get',
        'doctype': doctype,
        'fields': jsonEncode(['count(`tab$doctype`.name) as total_count']),
        'filters': filters != null ? jsonEncode(filters) : null
      });
  if (response.isSuccess) {
    Map resultAsMap = response.data!.message;
    // Since the response is an array of array
    return RequestResponse.success(resultAsMap['values']?.first?.first);
  }
  return RequestResponse.fail(handleError('get_doc_count', response.error));
}