setValue<T extends FrappeDocument> method

  1. @override
Future<RequestResponse<T?>> setValue<T extends FrappeDocument>(
  1. T obj,
  2. String? docName,
  3. String docField,
  4. dynamic docValue,
)
override

Returns the modified document T after setting the docField with docValue.

Returns a failure in case doctype, docName or docField don't exist.

Throws InvalidFrappeFieldValue if docValue doesn't comply with DBFilter.isDBValue.

Implementation

@override
Future<RequestResponse<T?>> setValue<T extends FrappeDocument>(
    T obj, String? docName, String docField, dynamic docValue) async {
  EmptyDoctypeError.verify(obj.doctype);
  EmptyDocNameError.verify(docName);
  EmptyDocFieldError.verify(docField);
  if (!DBFilter.isDBValue(docValue)) throw InvalidFrappeFieldValue();

  final params = SetValueParams(obj.doctype, docName, docField, docValue);

  params.cmd = 'frappe.client.set_value';

  final response = await Request.initiateRequest(
      url: config.hostUrl,
      method: HttpMethod.POST,
      contentType: ContentTypeLiterals.APPLICATION_JSON,
      data: params.toJson());
  if (response.isSuccess) {
    final temp = obj.fromJson<T>(response.data!.message);
    temp.rawResponse = response.data!.message;
    return RequestResponse.success(temp, rawResponse: response.rawResponse);
  }
  return RequestResponse.fail(handleError('set_value', response.error));
}