The-Commit-Company / frappe-react-sdk

React hooks for Frappe
https://frappe-react.vercel.app
MIT License
110 stars 36 forks source link

update a doctype with a child table [Help Needed] #17

Closed sutyum closed 1 year ago

sutyum commented 1 year ago

How can one append, delete items from the child table in a doctype using the useFrappeUpdate hook?

For instance i have a chat doctype with each message in the chat as a row in the child table messages. How would i append new messages here?

sumitjain236 commented 1 year ago

If you want to append items to child table then, use useFrappeCreateDoc and pass param as object of data of that child table with 3 more mandatory fields, parent - which is ID of parent document, parentfield - which is field name of parent doctype where the child table link and parenttype - which is name of parent doctype.

const { createDoc } = useFrappeCreateDoc()

const data = {
// fieldname of the child table - message, receiver, sender
message: 'Hi Harry',

receiver: 'Harry',

sender: 'Ron',

// ID of the parent document
parent: "3533e7c7c1",

// Field name of the parent document
parentfield: "connection",

// Doctype of the parent document
parenttype: "Parent",
}

const handleClick = () => {
// Child Table - Child
createDoc('Child', data)
}

For Delete and Update and Child document you can directly use useFrappeUpdateDoc and useFrappeDeleteDoc and pass Child doctype name as doctype.

Shanuka-98 commented 1 year ago

Hi @nikkothari22 @sumitjain236 , can we create a parent table and child table at the same time cause of the Mandatory fields required in the doc-type table?

in my case, Mandatory fields are required in Stock Entry. Items(child table)

nikkothari22 commented 1 year ago

Hey @Shanuka-98 , yeah you can. The SDK uses standard Frappe REST APIs.

If you want to create a new stock entry with items in child table, just use useFrappeCreateDoc and then in createDoc pass in the items as an array of objects.

When you want to update, make sure to pass in the name of the child table rows in the array to the updateDoc method of useFrappeUpdateDoc.

We usually use React Hook Form to handle our forms and use useFieldArray to handle child tables.

Shanuka-98 commented 1 year ago

Hi @nikkothari22, Thank you for the solution, it works now.