Error on Sales Invoice Submit due to custom script

We are receiving an error when we generate an invoice and Save it – probably due to the custom script. Can anyone see an issue since the last upgrade as that’s when the issue started? If it helps the Standard Reply code appears in the popup form on Submit but the actual text of the reply doesn’t. If you remove “Invoice” from the standard reply field and re-add it, the text then appears in the body of the email.

The error text is last below, but first this is the custom script within Sales Invoice. One section of it is designed to auto-insert the Standard Reply of “Invoice”:

frappe.ui.form.on("Sales Invoice", "refresh", function(frm){
var old_name = frm.docname, sales_order = frm.doc.items[0].sales_order.replace("M", "M-");
if (!frm.doc.__islocal && sales_order && frm.doc.name!==sales_order){
    frappe.call({
    method: 'frappe.model.rename_doc.rename_doc',
    args: {
        doctype: frm.doctype,
        old: frm.docname,
        "new": sales_order,
        "merge": false
   },
   callback: function(res){
     if (!res.exc){ 
       frappe.model.remove_from_locals(frm.doctype, old_name);
       frappe.set_route("Form", frm.doctype, sales_order);
     }
   }
});
}
});

// Below fetches the deposit and po_no fields from the Order onto Invoice

cur_frm.add_fetch('sales_order', 'deposit', 'deposit');

cur_frm.add_fetch('sales_order', 'po_no', 'po_no');

// Below sets the Email Standard Reply field to "Invoice"

cur_frm.email_doc = function(message) {
var composer = new frappe.views.CommunicationComposer({
doc: cur_frm.doc,
frm: cur_frm,
attach_document_print: true,
message: message,
real_name: this.doc.real_name || this.doc.contact_display || this.doc.contact_name
});
composer.dialog.get_input('standard_reply').val('Invoice').trigger('change');
composer.dialog.set_value('send_me_a_copy', false);
}

// Added by max.morais.dmm@gmail.com
cur_frm.cscript._on_submit = cur_frm.cscript.on_submit;
cur_frm.email_doc = function(message){
  debugger;
  frappe.call({
'method': 'frappe.client.get_value',
'args': {
  'doctype': 'Address',
  'filters': {
    'customer': cur_frm.doc.customer,
    'is_primary_address': 1
  },
  'fieldname': 'email_id'
},
callback: function(res){
  var composer = new frappe.views.CommunicationComposer({
                            doc: cur_frm.doc,
                            frm: cur_frm,
                            subject: __(cur_frm.doctype) + ': ' + cur_frm.docname,
                            recipients: (res.message && res.message.email_id) || cur_frm.doc.email || cur_frm.doc.email_id || cur_frm.doc.contact_email,
                            attach_document_print: true,
                            message: message,
                            real_name: cur_frm.doc.real_name || cur_frm.doc.contact_display || cur_frm.doc.contact_name
            });
  composer.dialog.get_input('standard_reply').val('Invoice').trigger('change');
  composer.dialog.set_value('send_me_a_copy', false);
}
  })
}
cur_frm.cscript.on_submit = function(doc, cdt, cdn){
  cur_frm.cscript._on_submit(doc, cdt, cdn);
}

Something went wrong
Close
Server Error: Please check your server logs or contact tech support.

Traceback (most recent call last):
File “/home/frappe/benches/bench-2017-07-07-a/apps/frappe/frappe/app.py”, line 56, in application
response = frappe.handler.handle()
File “/home/frappe/benches/bench-2017-07-07-a/apps/frappe/frappe/handler.py”, line 21, in handle
data = execute_cmd(cmd)
File “/home/frappe/benches/bench-2017-07-07-a/apps/frappe/frappe/handler.py”, line 52, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “/home/frappe/benches/bench-2017-07-07-a/apps/frappe/frappe/init.py”, line 913, in call
return fn(*args, **newargs)
File “/home/frappe/benches/bench-2017-07-07-a/apps/frappe/frappe/model/rename_doc.py”, line 39, in rename_doc
new = validate_rename(doctype, new, meta, merge, force, ignore_permissions)
File “/home/frappe/benches/bench-2017-07-07-a/apps/frappe/frappe/model/rename_doc.py”, line 113, in validate_rename
exists = frappe.db.sql(“select name from tab{doctype} where name=%s for update”.format(doctype=frappe.db.escape(doctype)), new)
File “/home/frappe/benches/bench-2017-07-07-a/apps/frappe/frappe/database.py”, line 142, in sql
self._cursor.execute(query, values)
File “/home/frappe/benches/bench-2017-07-07-a/env/lib/python2.7/site-packages/MySQLdb/cursors.py”, line 250, in execute
self.errorhandler(self, exc, value)
File “/home/frappe/benches/bench-2017-07-07-a/env/lib/python2.7/site-packages/MySQLdb/connections.py”, line 50, in defaulterrorhandler
raise errorvalue
OperationalError: (1213, ‘Deadlock found when trying to get lock; try restarting transaction’

Seems not related to a custom script.

Please create a git issue, https://github.com/frappe/erpnext/issues/new

Done, thankyou @saurabh6790

can you post what was the probem and your respective solution.

I have no solution - I got advised by @saurabh6790 to log as an issue on git - then they told me to log it here in discuss. It’s getting frustrating as it HAS to be an issue with the custom script, that’s what inserts the Standard Reply value of “Invoice” into the Email body.

@saurabh6790 any advances on why it isn’t a script error? What else could be causing it?

Thanks. Liam.

@monojoker Are you still getting the same error as above? The issue is related to the script for renaming invoice, which is designed very badly in my view. Such functionality like rename should never be executed in refresh event. “Refresh” events executed many times, whenever you open the form and form many other places.
Why do you want such renaming? You can name sales invoice based on sales_order while saving itself. OR if you want it for existing Sales Invoice, add the functionality via a button.

As you are getting the error before execution of email dialog creation, it is not working properly.

Hi Nabin, yes apologies it was written very long ago by Max Morais but it did the job at the time. As I cannot script myself I had to rely on him to help and was very grateful as it got us going.

I am happy to have it rename on Save or design a button but I did not think it was possible to do a button with Cloud hosted? Hence it was done with script. If we simply change the script to be on Save not on Refresh would that be best?

Thanks.

If you want to implement it on save, then it will a separate script, the current one will not work. But if you want to execute the script in a button, then just add a custom button and then in the code replace “refresh” by the fieldname of button.

Hi Nabin,

OK so I can’t write another script I’d have to get support or the community to help me. But first, is there an easy tutorial on how to create a button in ERPNext (remember I’m on cloud hosted - sorry to keep saying but I have to remind everyone as it can limit our options on customisation :slight_smile: ).

If so I’ll try that first as it sounds the easiest method.

So you’re sure it’s the rename script that’s causing the error? Hopefully a button can fix as it’s annoying and time consuming for the invoicing users currently.

Thanks again.

“Button” is a field type as other field, so you will able to create a button by creating a custom field.

I think so, at least the error message is indicating so.