Transfer custom field value from order to invoice

So we have a field called “description” in Sales Order and “custom_description” in Sales Invoice.

When creating an Invoice, how do I pull the Order’s description into the Invoice’s custom_description?

I read about add_fetch(), but creating an Invoice from an Order should not need an extra link_field, or am I getting this wrong?

If the two fields have the same name it should fetch properly. (custom_description in SO to custom_description in SI)

But if you want to fetch it, then you can only do so in a client script.

frappe.ui.form.on("Sales Invoice", {
    refresh: function(frm){
        if (frm.doc.items){
            var sales_order = frm.doc.items[0].prevdoc_docname
            If (sales_order){
                frappe.db.get_doc("Sales Order", sales_order).then(function(r){
                    frm.set_value("custom_description", r.message.description)
                })
            }
        }
    }
})

Or something like that…

1 Like

@Void_Moon if you are creating invoice from sales order with using

Sales Order
Create > Invoice

Sales Invoice
Get Items From > Sales Order

This core function uses mapper method which auto map same field names and fetch data, so if in can case you want to use same data in all the documents, better to create field with same fieldname in all the documents.

1 Like

Thanks Void_Moon, that looks very good indeed!

frm.doc.items[0].prevdoc_docname

…is way cool! Didn’t know that existed.

Is there a good place for javascript-wise documentation or is it best to just look at the source?

@Hardik_Gadesha The Mapper Method works with the field with identical names, isn’t it?

@aa_prashant Yes it is, this is best practice to use.

1 Like

In Quotes and Orders, our custom “description”-field got named “description”. When adding it to Invoices however, it appeared as “custom-description”, hence the different names.

As I couldn’t make Void_Moon’s approach work (no property “prevdoc_docname” found) I am thinking of adding matching fields named “custom-description” to Quotes and Orders and moving over the information via SQL.

So, following procedure:

  • add custom field in Quotation, call it “custom-description”
  • add custom field in Sales Order, call it “custom-description”
UPDATE tabQuotation SET custom-description = description;
UPDATE `tabSales Order` SET custom-description = description;
  • Delete old “description”-fields in Quotation and Sales Order
  • Change list setup to show new descriptions

What do you guys think?

But recently, the ‘custom’ word is being added to field names by default for all custom fields. and that is why he has different field names.

The prevdoc_docname is set only if you create the SI from the SO via create->SI button in SO.
I’m not sure if that’s what you’re doing and still unable to get the value. Have your tried inspecting with cur_frm.doc to see all the attributes attached to the frm?
I might’ve misspelled it, cause I’ve used it multiple times and it should work…

As for your solution, yeah that should work as well… But ensure everywhere this custom_description field is linked with also changes to description. (Like in the customize form of the SI).

But in retrospect, shouldn’t there already be a description field in all item tables? Thereby fetching the description value directly?

@Void_Moon In SO => Create => SI I did:

JSON.stringify( cur_frm.doc )

and then searched for “prev” with CTRL-F but to no avail. Maybe it’s because we’re on 14.49.0 and frappe 14.56.1. as we only get cur_frm and not frm.

On the other hand there must be some reference to the previous document somewhere. But anyhow, I will try the other approach in a test installation now.

And you are absolutely right that there already are description fields for items! Ours is for the process in general so you get a hint of what it was in list views of Quotations, SO and SI.

Cheers,
k

I also have similar requirements but I don’t understand how to do it.