Values and counts#
When you need one or two fields rather than a whole document, these calls are
cheaper and clearer than get_doc.
Reading fields with get_value#
row = client.get_value("Customer", "customer_group", "Acme Corporation")
print(row) # {"customer_group": "Commercial"}
The third argument is either a document name or filters. With filters, the first matching document is used:
row = client.get_value("Customer", ["name", "territory"], {"email_id": "ops@acme.example"})
if row is None:
print("no customer with that email")
else:
print(row["name"], row["territory"])
| Argument | Meaning |
|---|---|
doctype |
DocType to read. |
fieldname |
One field name, or a list of them. |
filters |
A document name, or filters in any supported form. |
It returns a dict of the requested fields, or None when nothing
matches. It never raises NotFoundError for a missing match, which makes it
the natural "does this exist?" check.
Changing fields with set_value#
saved = client.set_value("ToDo", "a1b2c3d4e5", {"status": "Closed", "priority": "Low"})
print(saved["status"])
set_value loads the document, sets the fields, saves it with full validation
and returns the saved document. The result is the same as
update; it goes through the
frappe.client.set_value method instead of the resource route.
Counting documents#
open_todos = client.get_count("ToDo", {"status": "Open"})
all_customers = client.get_count("Customer")
get_count returns an int and counts only documents the user may read.
Upsert: create or update#
A common pattern combines get_value with insert and update:
def upsert_customer(client, email, values):
existing = client.get_value("Customer", "name", {"email_id": email})
if existing:
return client.update("Customer", existing["name"], values)
return client.insert("Customer", {"email_id": email, **values})
Two programs running this at the same moment can both see "no customer" and
both insert. If that matters, add a unique constraint on the field in Frappe
(Customize Form → Unique) so the second insert raises ConflictError
or ValidationError, then retry the lookup.