Skip to content

Conversation

@elshafei-developer
Copy link
Contributor

When creating a Journal Entry or Stock Reconciliation that contains more than 100 rows and submitting it before saving, ERPNext automatically enqueues the submission process as a Background Job.

	# file: erpnext/erpnext/accounts/doctype/journal_entry/journal_entry.py
	def submit(self):
		if len(self.accounts) > 100:
			msgprint(_("The task has been enqueued as a background job."), alert=True)
			self.queue_action("submit", timeout=4600)
		else:
			return self._submit()

However, if the document has not yet been saved, the Background Job fails with a DoesNotExistError because the system cannot find the corresponding Journal Entry record in the database.

In another scenario, if the document is saved but modified before submission, Background Job will submit the old document, not the new, modified version.

The Background Job attempts to load the document using:
doc = frappe.get_doc(__doctype, __name)

However, when submission is enqueued as a Background Job, the document must exist in the database before the job starts.
Otherwise, the queued task cannot find it and fails.

Note: The _submit() method itself calls self.save() after setting the document status to Submitted

# File: frappe/model/document.py
def _submit(self):
    """Submit the document. Sets `docstatus` = 1, then saves."""
    self.docstatus = DocStatus.SUBMITTED
    return self.save()
Click to view full error traceback Traceback (most recent call last): File "/workspace/development/frappe-bench/env/lib/python3.11/site-packages/rq/worker.py", line 1428, in perform_job rv = job.perform() ^^^^^^^^^^^^^ File "/workspace/development/frappe-bench/env/lib/python3.11/site-packages/rq/job.py", line 1278, in perform self._result = self._execute() ^^^^^^^^^^^^^^^ File "/workspace/development/frappe-bench/env/lib/python3.11/site-packages/rq/job.py", line 1315, in _execute result = self.func(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/workspace/development/frappe-bench/apps/frappe/frappe/utils/background_jobs.py", line 225, in execute_job retval = method(**kwargs) ^^^^^^^^^^^^^^^^ File "/workspace/development/frappe-bench/apps/frappe/frappe/model/document.py", line 1711, in execute_action doc = frappe.get_doc(__doctype, __name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/workspace/development/frappe-bench/apps/frappe/frappe/__init__.py", line 1308, in get_doc return frappe.model.document.get_doc(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/workspace/development/frappe-bench/apps/frappe/frappe/model/document.py", line 85, in get_doc return controller(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/workspace/development/frappe-bench/apps/erpnext/erpnext/accounts/doctype/journal_entry/journal_entry.py", line 114, in __init__ super().__init__(*args, **kwargs) File "/workspace/development/frappe-bench/apps/erpnext/erpnext/controllers/accounts_controller.py", line 101, in __init__ super().__init__(*args, **kwargs) File "/workspace/development/frappe-bench/apps/frappe/frappe/model/document.py", line 126, in __init__ self.load_from_db() File "/workspace/development/frappe-bench/apps/frappe/frappe/model/document.py", line 179, in load_from_db frappe.throw( File "/workspace/development/frappe-bench/apps/frappe/frappe/__init__.py", line 609, in throw msgprint( File "/workspace/development/frappe-bench/apps/frappe/frappe/__init__.py", line 574, in msgprint _raise_exception() File "/workspace/development/frappe-bench/apps/frappe/frappe/__init__.py", line 525, in _raise_exception raise exc frappe.exceptions.DoesNotExistError: Journal Entry None not found

backport version-15-hotfix

@github-actions github-actions bot added the needs-tests This PR needs automated unit-tests. label Oct 14, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 14, 2025

📝 Walkthrough

Walkthrough

Two submit methods were modified to call self.save() before enqueueing background processing when the workload threshold is exceeded. In erpnext/accounts/doctype/journal_entry/journal_entry.py, JournalEntry.submit now calls self.save() before enqueueing if there are more than 100 accounts. In erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py, StockReconciliation.submit now calls self.save() before enqueueing if there are more than 100 items. Enqueue behavior, messaging, and other control paths remain unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Files to inspect: erpnext/accounts/doctype/journal_entry/journal_entry.py, erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py
  • Pay attention to:
    • Ordering of self.save() vs enqueue and any transactional context or implicit assumptions about persistence.
    • Side effects triggered by save() (hooks, validations, observers) during submit.
    • Consistency of the threshold (100) and any related messaging or queue identifiers.

Suggested reviewers

  • ruthra-kumar
  • rohitwaghchaure
  • mihir-kandoi
  • diptanilsaha

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding self.save() before background job enqueuing for Journal Entry and Stock Reconciliation with >100 rows to prevent DoesNotExistError.
Description check ✅ Passed The description is directly related to the changeset, providing detailed context about the bug (unsaved documents failing when submitted as background jobs), the root cause, and the solution implemented.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 07ef7fd and 10deb4b.

📒 Files selected for processing (1)
  • erpnext/accounts/doctype/journal_entry/journal_entry.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • erpnext/accounts/doctype/journal_entry/journal_entry.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
  • GitHub Check: Python Unit Tests (4)
  • GitHub Check: Python Unit Tests (3)
  • GitHub Check: Python Unit Tests (2)
  • GitHub Check: Python Unit Tests (1)
  • GitHub Check: Patch Test
  • GitHub Check: Summary

Tip

📝 Customizable high-level summaries are now available in beta!

You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.

  • Provide your own instructions using the high_level_summary_instructions setting.
  • Format the summary however you like (bullet lists, tables, multi-section layouts, contributor stats, etc.).
  • Use high_level_summary_in_walkthrough to move the summary from the description to the walkthrough section.

Example instruction:

"Divide the high-level summary into five sections:

  1. 📝 Description — Summarize the main change in 50–60 words, explaining what was done.
  2. 📓 References — List relevant issues, discussions, documentation, or related PRs.
  3. 📦 Dependencies & Requirements — Mention any new/updated dependencies, environment variable changes, or configuration updates.
  4. 📊 Contributor Summary — Include a Markdown table showing contributions:
    | Contributor | Lines Added | Lines Removed | Files Changed |
  5. ✔️ Additional Notes — Add any extra reviewer context.
    Keep each section concise (under 200 words) and use bullet or numbered lists for clarity."

Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@stale
Copy link

stale bot commented Oct 30, 2025

This pull request has been automatically marked as inactive because it has not had recent activity. It will be closed within 3 days if no further activity occurs, but it only takes a comment to keep a contribution alive :) Also, even if it is closed, you can always reopen the PR when you're ready. Thank you for contributing.

@stale stale bot added the inactive label Oct 30, 2025
@stale stale bot removed the inactive label Oct 30, 2025
@elshafei-developer
Copy link
Contributor Author

Hi @diptanilsaha
Could you please review these PR and provide any feedback?

@diptanilsaha
Copy link
Member

Can you explain the use case?

@AmalSharqIT
Copy link

in the following code if data is bigger that 100 the submit will fail.

journal_entry = frappe.new_doc("Journal Entry")
journal_entry.set("accounts", [data])
journal_entry.submit()

in the following code if data is bigger that 100 the submit will not fail but the is_system_generated parameter will not save in the document.

journal_entry = frappe.new_doc("Journal Entry")
journal_entry.set("accounts", [data])
journal_entry.save()
journal_entry.is_system_generated = 1
journal_entry.submit()

the same will apply on Stock Reconciliation DocType.
With the PR change, the code will save the document before sending it to the queue if bigger that 100 and if smaller, the _submit function will save the document.

@diptanilsaha
Copy link
Member

Thanks @AmalSharqIT.

@diptanilsaha diptanilsaha self-assigned this Nov 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-tests This PR needs automated unit-tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants