useDoctype ​

useDoctype creates, saves, deletes and runs methods on documents of one DocType, without fetching any. Use it next to useDoc or useList, which do the reading.

Basic example ​

vue
<script setup>
import { useDoctype } from 'frappe-ui'

const todo = useDoctype('ToDo')

async function create(description) {
  const doc = await todo.insert.submit({ description })
  console.log(doc.name)
}

async function close(name) {
  await todo.setValue.submit({ name, status: 'Closed' })
}
</script>

Each submit sends its own request. Closing two documents at once, or one document twice, does not cancel either request.

Run a method ​

runDocMethod calls a method on one document. runMethod calls a method on the DocType itself:

vue
<script setup>
import { useDoctype } from 'frappe-ui'

const user = useDoctype('User')

async function resetPassword(name) {
  await user.runDocMethod.submit({
    name,
    method: 'reset_password',
    params: { send_email: true },
  })
}

async function loadOnlineUsers() {
  await user.runMethod.submit({ method: 'get_online_users' })
}
</script>

Options ​

The DocType is the first argument: useDoctype(doctype, options?).

NameTypeDefaultDescription
doctypestringrequiredThe DocType every member acts on.
options.baseUrlstring''A prefix for every request URL.

Return value ​

NameTypeDescription
insertwrite memberinsert.submit(values) creates a document and resolves with it. insert.isLoading() takes no argument.
setValuewrite membersetValue.submit({ name, ...values }) saves fields of one document. setValue.isLoading(name) checks one document.
deletewrite memberdelete.submit({ name }) deletes one document. delete.isLoading(name) checks one document.
runDocMethodwrite memberrunDocMethod.submit({ name, method, params?, validate? }) calls a whitelisted method on one document. runDocMethod.isLoading(name, method) checks one pair.
runMethodwrite memberrunMethod.submit({ method, params?, validate? }) calls a whitelisted method on the DocType. runMethod.isLoading(method) checks one method.

Every write member has these members:

NameTypeDescription
dataT | nullThe response of the submit that started last.
errorError | nullThe error of the submit that started last.
loadingbooleantrue while any submit of this member is in flight.
submit(params)(params) => PromiseSends the request. Resolves with the response, or rejects with the error.
isLoading(...)(...) => booleantrue while a submit for one target is in flight, so a list can show it on one row.

An older submit that settles after a newer one still resolves or rejects for its own caller, but does not change data or error.

setValue and delete also update the document in every useDoc and useList that shows it.

Errors ​

submit() rejects with a FrappeResponseError when the server returns an error. On runDocMethod and runMethod, a validate function runs first. If it returns a string, submit() rejects with that message and sends no request.