Client side doc creation (posting_date)

I am trying to create a new Sales Invoice doc via client side scripting, but I am struggling quite a bit. Most immediately, I’m having trouble setting either the posting_date or the due_date programmatically. My code is below:

frappe.ui.form.on('Program Enrollment', {
    refresh: function(frm) {
	    frm.add_custom_button(__('Generate invoice'), function() {
		console.log(frm.doc.enrollment_date);
		console.log(frappe.datetime.get_today());
		frappe.run_serially([
		    () => frappe.route_options = {
			"posting_date": frm.doc.enrollment_date,
			"due_date": frappe.datetime.get_today(),
			"company": "Kula Culture Management Training, Pvt. Ltd."
		    },
		    () => frappe.new_doc("Sales Invoice")
		]);
	    });
    }
});

Company sets fine, and both of the console.log calls return results that I would expect to work (similar to “2019-05-24”). Any ideas?

1 Like

Hi, did you ever manage to do this? I am trying to do a similar thing to create a Delivery Note from a submitted Sales Order but it only seems to pass values that are being used in the standard filter.

Hi @archais,

We never figured out to do it effectively with the route options, but we have had better luck with a different approach creating the document directly with frappe.db.insert:

            var order = {
                'doctype': 'Sales Invoice',
                'customer': cur_frm.doc.customer,
                'currency': "USD",
                'items': [],
                'taxes': [],
                'payments': [],
                'is_pos': 1,
                'due_date': cur_frm.doc.due_date
            }
            
            order['items'].push({
            	'doctype': 'Sales Invoice Item',
            	'item_code': 'ITM-000',
            	'item_name': 'Sample Item',
            	'uom': 'Unit',
            	'conversion_factor': 1.00,
            	'rate': 1000,
            	'qty': 1.00
            })
            
            order['taxes'].push({
            	'doctype': 'Sales Taxes and Charges',
            	'charge_type': "On Net Total",
            	'account_head': "VAT - KC",
            	'description': "VAT (13%)<br>",
            	'rate': 13.00
            })
            
            order['payments'].push({
                'doctype': 'Sales Invoice Payment',
                'mode_of_payment': 'Cash',
                'amount': 1130.00
            })
            
            frappe.db.insert(order).then(doc => {
                console.log("Inserted");
            	frm.save_or_update();
            	frappe.set_route("Form", "Sales Invoice", doc.name)
            })

It’s been working consistently for us, but it may or may not work for your case. Let me know if I can clarify anything!

4 Likes

Hi thank you. About half an hour after I had asked I actually found a script for copying between child tables but just as I was about to test it, my boss decided that he didn’t want automatic delivery notes.:sob::joy: