cloudy-cyan
cloudy-cyan11mo ago

Subform to create related record

Using Supabase as a data provider and following the exemple in the CRM app, I am trying to create a related record as it is done in this exemple CRM app, however the data variable return in https://github.com/refinedev/refine/blob/8066c0c1d8bba422e1b38ee325a6f4bca811e95d/examples/app-crm/src/routes/companies/create.tsx#L110 is empty, so I can’t set the companyId using createdCompany. Is there anything special to do when using a different data provider than the one used in the CRM exemple app to get the id of the newly created record in a onFinish callback ?
GitHub
refine/examples/app-crm/src/routes/companies/create.tsx at 8066c0c1...
A React Framework for building internal tools, admin panels, dashboards & B2B apps with unmatched flexibility. - refinedev/refine
5 Replies
kapa.ai
kapa.ai11mo ago
Hi @jibize. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hmm, I don't know enough to give you a confident answer yet. However, based on the information provided, it seems like you're trying to create a related record and retrieve the ID of the newly created record in the onFinish callback. In the provided code snippets, the onFinish function is used to handle form submission. When the form is submitted, onFinish is called with the form values. In the edit action, onFinish calls the useUpdate hook with the form values, and in the clone action, it calls the useCreate hook. The useUpdate and useCreate hooks then call the dataProvider's update and create functions respectively and return the response. The onSuccess or onError function is then called with the response, depending on the response status. If you're using a different data provider, you might need to adjust how you're handling the response in the onFinish function. The response might be structured differently depending on the data provider, so you might need to access the ID of the newly created record differently. For more information on how to use the useForm, useUpdate, and useCreate hooks, you can refer to the Refine documentation (https://refine.dev/docs/data/hooks/use-form). For more specific guidance, you might want to check the documentation or support resources for your specific data provider. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai11mo ago
<@740787797765521508> kapa.ai is still learning and improving, please let us know how it did by reacting below
cloudy-cyan
cloudy-cyanOP11mo ago
Thank you @kapa.ai, is the onMutationSuccess function callback a good place to create a related record in a subform ?
kapa.ai
kapa.ai11mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, the onMutationSuccess function callback can be a good place to create a related record in a subform. This function is called after a successful mutation, such as creating or updating a record. The onMutationSuccess function receives several parameters, including data, which is the returned value from useCreate or useUpdate depending on the action. This means you can access the ID of the newly created or updated record in this function. Here's an example of how you might use it:
useForm({
refineCoreProps: {
onMutationSuccess: (data, variables, context, isAutoSave) => {
console.log({ data, variables, context, isAutoSave });
// You can use the `data` parameter to access the ID of the newly created record
// and then use it to create a related record in a subform.
},
},
});
useForm({
refineCoreProps: {
onMutationSuccess: (data, variables, context, isAutoSave) => {
console.log({ data, variables, context, isAutoSave });
// You can use the `data` parameter to access the ID of the newly created record
// and then use it to create a related record in a subform.
},
},
});
Remember that the exact structure of the data parameter might depend on your data provider, so you might need to adjust how you're accessing the ID. For more information on how to use the onMutationSuccess function, you can refer to the Refine documentation (https://refine.dev/docs/data/hooks/use-form#onmutationsuccess). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai11mo ago
<@740787797765521508> kapa.ai is still learning and improving, please let us know how it did by reacting below

Did you find this page helpful?