Help needed with custom script

Hi,

I’ve done some custom code to create a bank entry from the expense claim but it doesn’t seem to be working and there’s no error in the console. Could someone please have a look and suggest where I might need to make changes?

JS Code

frappe.ui.form.on("Expense Claim", {
refresh: function(frm,dt,dn) {	
//cur_frm.cscript.custom_validate(frm.doc);

	if(frm.doc.docstatus == 0) {
	//	if(doc.status != 'Closed') {
			cur_frm.add_custom_button(__('Make Advance Payment'), cur_frm.cscript.make_advance_entry, __("Make"));
//cur_frm.cscript.make_journal_entry, __("Make"));
	//	}
		cur_frm.page.set_inner_btn_group_as_primary(__("Make"));	
	}
  },
make_advance_entry: function() {
		var me = this;
		return frappe.call({
			method: "erpnext.hr.doctype.expense_claim.expense_claim.make_advance_entry",
			args: {
				"docname": cur_frm.doc.name,
			},
			callback: function(r) {
				var doc = frappe.model.sync(r.message);
				frappe.set_route('Form', 'Journal Entry', r.message.name);
			}
		});
	},
});

PY Code

@frappe.whitelist()
def make_advance_entry(docname):
	from erpnext.accounts.doctype.journal_entry.journal_entry import get_default_bank_cash_account
        self.validate_account_details()

	expense_claim = frappe.get_doc("Expense Claim", docname)
	default_bank_cash_account = get_default_bank_cash_account(expense_claim.company, "Bank")
	if not default_bank_cash_account:
		default_bank_cash_account = get_default_bank_cash_account(expense_claim.company, "Cash")

	je = frappe.new_doc("Journal Entry")
	je.voucher_type = 'Bank Entry'
	je.company = expense_claim.company
	je.remark = 'Advance Payment against Expense Claim: ' + docname;

	je.append("accounts", {
		"account": expense_claim.advance_account,
		"debit_in_account_currency": flt(expense_claim.total_sanctioned_amount),
		"reference_type": "Expense Claim",
		"reference_name": expense_claim.name
	})

	je.append("accounts", {
		"account": default_bank_cash_account.account,
		"credit_in_account_currency": flt(expense_claim.total_sanctioned_amount),
		"reference_type": "Expense Claim",
		"reference_name": expense_claim.name,
		"balance": default_bank_cash_account.balance,
		"account_currency": default_bank_cash_account.account_currency,
		"account_type": default_bank_cash_account.account_type
	})

	return je.as_dict()


def validate_account_details(self):
		if not self.advance_account:
			frappe.throw(_("Please set Advance Account for the Employee"))

Thanks

When I click on the button, nothing seems to happen

A couple suggestions:

  1. Don’t add code to the ERPNext module directly. This will mean you can’t run updates on ERPNext ever. Instead, create your own app and add the code in there.
  2. Adding debugger; or console.log('message'); code in your js code will make it a lot easier to see what’s going on (is the code getting called at all, what are the values, etc)
  3. You probably want to pass frm into your function for make_advance_entry.
  4. It doesn’t look like you save the je. I think you’ll need a je.save() at the end.
1 Like

Hi @Ben_Cornwell_Mott

Thanks for your response. I’m only getting a timeout error in the log:

sport=polling&t=LpN3cC- net::ERR_CONNECTION_TIMED_OUT
i.create @ libs.min.js:8008
i @ libs.min.js:8008
o.request @ libs.min.js:8008
o.doPoll @ libs.min.js:8008
n.poll @ libs.min.js:8008
n.doOpen @ libs.min.js:8008
n.open @ libs.min.js:8008
n.open @ libs.min.js:8007
n @ libs.min.js:8007
n @ libs.min.js:8007
n.open.n.connect @ libs.min.js:8007
(anonymous) @ libs.min.js:8007
libs.min.js:8008 GET http://xxxxxxxxxx.com:9001/socket.io/?EIO=3&transport=polling&t=LpN3fdk net::ERR_CONNECTION_TIMED_OUT
i.create @ libs.min.js:8008
i @ libs.min.js:8008
o.request @ libs.min.js:8008
o.doPoll @ libs.min.js:8008
n.poll @ libs.min.js:8008
n.doOpen @ libs.min.js:8008
n.open @ libs.min.js:8008
n.open @ libs.min.js:8007
n @ libs.min.js:8007
n @ libs.min.js:8007
n.open.n.connect @ libs.min.js:8007
(anonymous) @ libs.min.js:8007
libs.min.js:8008 GET http://xxxxxxxxxxx.com:9001/socket.io/?EIO=3&transport=polling&t=LpN3j2U net::ERR_CONNECTION_TIMED_OUT
i.create @ libs.min.js:8008
i @ libs.min.js:8008
o.request @ libs.min.js:8008
o.doPoll @ libs.min.js:8008
n.poll @ libs.min.js:8008
n.doOpen @ libs.min.js:8008
n.open @ libs.min.js:8008
n.open @ libs.min.js:8007
n @ libs.min.js:8007
n @ libs.min.js:8007
n.open.n.connect @ libs.min.js:8007
(anonymous) @ libs.min.js:8007

Could you please suggest exactly how to pass frm in the code? I’m following the reference of the make_bank_entry code in ERPNext and I’m not sure that was included

Thanks for your help

It doesn’t appear as though the code is getting called at all

Any ideas please?

What would call make_advance_entry? Do you have a button with that label added as a custom field? You could also use a frm.add_custom_button() to call it. There are good examples in the core code for this.

Hi @Ben_Cornwell_Mott

Yes, I have a custom button that calls that function. Please see my js code as pasted above

Thanks

I expect this part of the code to call the make_advance_entry function but it doesn’t seem to be doing so. I also tried replacing it with this.make_advance_entry but it’s still the same

Hi all,

Any help in debugging this would be greatly appreciated

Thanks

Hi @Ben_Cornwell_Mott

Trust you’re doing great. I eventually got the script to work but I need help with one more thing. I have a custom field on the Expense Claim form and would like to add a validation to the custom button so that an error message is returned to the user if they click the button and the custom field is blank. Any idea how to achieve this?

Thanks