My child table data disappears after a few seconds

Context:

I’m working on implementing a custom approval trail (sub-workflow) for a DocType using a custom API. The sub-workflow is supposed to kick in once the document reaches the Pending state (i.e., after it is submitted).

I’ve created a custom function create_approval_trail(doc, approvers, preserve_history=False) that appends entries to a child table field called approval_trail. It works fine initially — the table gets populated with approver data correctly.

Issue:

After submitting the document and entering the Pending state:

  • The approval_trail child table gets populated (as expected).
  • But within a few seconds, the entries disappear — the table becomes empty again.
  • There is no error in the UI or console, but the child table does not persist the data.

Expected Behavior:

  • Once the document is submitted and enters the Pending state,
  • The approval_trail child table should remain populated with the list of approvers until the document is either approved or rejected.

Current Workflow States:

  • Draft (initial)
  • Pending (starts sub-workflow / approval trail)
  • Approve
  • Reject

Code Overview:

Here’s the core part of the create_approval_trail function:

python

CopyEdit

def create_approval_trail(doc, approvers, preserve_history=False):
    if not preserve_history:
        doc.approval_trail = []
    else:
        doc.approval_trail.append({
            'status': 'Resubmitted',
            'comments': 'Document resubmitted with changes',
            ...
        })

    for approver in approvers:
        user = get_approver_user(approver, doc)
        if user:
            doc.append('approval_trail', {
                'user': user,
                'status': 'Pending',
                ...
            })

What I’ve Checked:

  • The data is definitely being appended to doc.approval_trail.
  • The code runs during the transition from Draft → Pending.
  • I suspect something is resetting or overwriting the child table data post-submit (maybe another before_save or on_update_after_submit?).

Question:

  • Why is my approval_trail child table data disappearing after a few seconds?
  • Could this be related to the timing of the workflow state transition vs document save/submit events?
  • Do I need to explicitly doc.save() or doc.db_update() somewhere after appending to the child table?

Let me know if you need my full hook or method trace.


Table Data disappear video (Loom link)

Hey,

on which hook you are calling this create_approval_trail?

  • check your network tab, does any API calling which is returning empty child list.
  • On your hook print on first line when this method is actually calling. Is it calling twice?

Well this worked for me for now

doc.save()

Complete code

def create_approval_trail(doc, approvers, preserve_history=False):
    if not preserve_history:
        doc.approval_trail = []
    else:
        doc.approval_trail.append({
            'status': 'Resubmitted',
            'comments': 'Document resubmitted with changes',
            ...
        })

    for approver in approvers:
        user = get_approver_user(approver, doc)
        if user:
            doc.append('approval_trail', {
                'user': user,
                'status': 'Pending',
                ...
            })
  doc.save()