ERPNext v12 – Prevent Server Scripts from Running During Data Import
Issue
In ERPNext v12, Server Scripts (Before Save / After Save) may still execute during Data Import, even when using common checks like:
-
frappe.flags.in_import -
frappe.flags.in_importer -
frappe.flags.mute_emails
These global flags are not reliable inside safe_exec, causing unwanted side effects such as auto ToDo creation or workflow automation during imports.
Finding
During Data Import, ERPNext consistently sets the following flag on the document:
doc.flags.updater_reference.label == "via Data Import"
This value is:
-
Always present for imported records
-
Accessible inside Server Scripts
-
Reliable across doctypes
Solution
Use this guard condition to safely skip automation during imports:
skip_automation = False
try:
if doc.flags.updater_reference and doc.flags.updater_reference.get("label") == "via Data Import":
skip_automation = True
except:
pass
if not skip_automation:
# automation logic
Result
-
Automation skipped during Data Import
-
Normal behavior on manual saves
-
Fully safe-exec compatible
This approach has proven to be the most reliable method in ERPNext v12.