Hello,
1.I’m using a custom script in ERPNext to save all unsaved records of a specific doctype after importing. The script shows success messages, but the records aren’t saved.
- Earlier I was trying to do it by pressing a custom button on the record “Save all imported Documents”. Even after showing the notifications of save it was only saving the that document on which the process was triggered
I used Chatgpt to write this code for me as I don’t how to write codes
Any guidance or assistance to resolve this issue would be greatly appreciated.
Thank you!
I am using this code
window.onload = function() {
if (cur_page.page_name === ‘YourDoctypeName’ && cur_list) {
save_all_imported_records();
}
};
function save_all_imported_records() {
frappe.call({
method: ‘frappe.client.get_list’,
args: {
doctype: ‘YourDoctypeName’,
filters: { docstatus: 0 },
limit_page_length: 500
},
callback: function(response) {
let records = response.message;
if (records && records.length > 0) {
save_records_sequentially(records, 0);
} else {
console.log(‘No unsaved records found.’);
}
}
});
}
function save_records_sequentially(records, index) {
if (index < records.length) {
let record = records[index];
frappe.call({
method: ‘frappe.client.get’,
args: {
doctype: ‘YourDoctypeName’,
name: record.name
},
callback: function(r) {
let doc = r.message;
if (doc && !doc.__islocal) {
frappe.model.with_doc(‘YourDoctypeName’, doc.name, function() {
let d = frappe.get_doc(‘YourDoctypeName’,
For the second method which only saving 1 record on which the process was triggered was this
frappe.ui.form.on(‘YourDoctypeName’, {
refresh: function(frm) {
frm.add_custom_button(__(‘Save All Imported Records’), function() {
save_all_imported_records();
});
}
});
function save_all_imported_records() {
frappe.call({
method: ‘frappe.client.get_list’,
args: {
doctype: ‘YourDoctypeName’,
filters: { docstatus: 0 }, // Adjust filters as needed
limit_page_length: 500 // Adjust as needed
},
callback: function(response) {
let records = response.message;
if (records && records.length > 0) {
process_records_sequentially(records, 0);
} else {
frappe.msgprint(__(‘No unsaved records found.’));
}
}
});
}
function process_records_sequentially(records, index) {
if (index < records.length) {
let record = records[index];
frappe.call({
method: ‘frappe.client.get’,
args: {
doctype: ‘YourDoctypeName’,
name: record.name
},
callback: function(r) {
let doc = r.message;
if (doc && !doc.__islocal) {
frappe.model.with_doc(‘YourDoctypeName’, doc.name, function() {
let d = frappe.get_doc(‘YourDoctypeName’, doc.name);
d.unsaved = true;
frappe.call({
method: ‘frappe.desk.form.save.savedocs’,
args: {
doc: d,
action: ‘Save’
},
callback: function(save_response) {
if (!save_response.exc) {
console.log(Record ${record.name} saved successfully.
);
}
// Process the next record
process_records_sequentially(records, index + 1);
}
});
});
} else {
// Process the next record if the document is already saved or local
process_records_sequentially(records, index + 1);
}
}
});
} else {
frappe.msgprint((‘All records processed.’));
}
}