I want to create an Import button like image in the view menu of my Staff doctype. I want when I click on it, it will allow me to import the excel file. That file will be saved into frappe’s core File doctype and then update the data into my Staff doctype. Please help me.
ejaaz
October 18, 2024, 6:24am
2
Code to add custom button in list view
frappe.listview_settings['Staff'] = {
refresh: function(listview) {
listview.page.add_inner_button("Import", function() {
// your logic here
});;
},
};
Data import logic, you can get from this file.
}
},
});
}
},
});
var create_import_button = function (frm) {
frm.page
.set_primary_action(__("Import"), function () {
return frappe.call({
method: "erpnext.accounts.doctype.chart_of_accounts_importer.chart_of_accounts_importer.import_coa",
args: {
file_name: frm.doc.import_file,
company: frm.doc.company,
},
freeze: true,
freeze_message: __("Creating Accounts..."),
callback: function (r) {
if (!r.exc) {
clearInterval(frm.page["interval"]);
2 Likes
I use vuejs to create that button, can I use it?
The list view you are seeing was created by me using frappe-ui, not the list view in core frappe.
ejaaz
October 18, 2024, 6:49am
5
I don’t have any idea about frappe-ui, sorry about that.
In this case, I can only help you with the logic to import data. The code below imports data from a CSV, so this script may help you import data.
msg += _("Please import accounts against parent company or enable {} in company master.").format(
frappe.bold(_("Allow Account Creation Against Child Company"))
)
frappe.throw(msg, title=_("Wrong Company"))
if frappe.db.get_all("GL Entry", {"company": company}, "name", limit=1):
return False
@frappe.whitelist()
def import_coa(file_name, company):
# delete existing data for accounts
unset_existing_data(company)
# create accounts
file_doc, extension = get_file(file_name)
if extension == "csv":
data = generate_data_from_csv(file_doc)
else:
data = generate_data_from_excel(file_doc, extension)
Can you summarize for me an import flow in frappe core? Thank you very much.
Can you take a picture to show me where the button is?
ejaaz
October 18, 2024, 7:10am
8
It is hard to explain whole process I will share some frappe core data import code links.
return frappe.get_doc("Data Import", data_import).get_preview_from_template(
import_file, google_sheets_url
)
@frappe.whitelist()
def form_start_import(data_import: str):
return frappe.get_doc("Data Import", data_import).start_import()
def start_import(data_import):
"""This method runs in background job"""
data_import = frappe.get_doc("Data Import", data_import)
try:
i = Importer(data_import.reference_doctype, data_import=data_import)
i.import_data()
except JobTimeoutException:
frappe.db.rollback()
data_import.db_set("status", "Timed Out")
except Exception:
frappe.db.rollback()
# set user lang for translations
frappe.cache.hdel("lang", frappe.session.user)
frappe.set_user_lang(frappe.session.user)
# set flags
frappe.flags.in_import = True
frappe.flags.mute_emails = self.data_import.mute_emails
self.data_import.db_set("template_warnings", "")
def import_data(self):
self.before_import()
# parse docs from rows
payloads = self.import_file.get_payloads_for_import()
# dont import if there are non-ignorable warnings
warnings = self.import_file.get_warnings()
warnings = [w for w in warnings if w.get("type") != "info"]
if warnings:
ejaaz
October 18, 2024, 7:11am
9
Datnguyen2k:
where the button is?
This button is inside Chart of Accounts Importer
Thank you, I have found its location. But this is not about creating a field in the doctype file for import. I want to create a new FileUploader in frapper-ui (link: Frappe UI ) to import data.