How do you populate custom fields from Item to Sale Order Item on Sale Order?

This question really applies to all child doctypes, but it was easier to ask using the Sale Order as an example because its well-known.

I added a custom field to both Item and Sale Order Item. The field has the same name in both doctypes.

When I add an Item to the Sale Order, the value from my custom field does not get added to the Sale Order Item.

I read one place where someone said this is supposed to happen automatically if the field names are the same. However, this does not seem to be the case.

How do you do this?

Is it possible what you saw was talking about linked docs (i.e., Sales OrderSales Invoice), which do show this behavior? I’m not aware of anything like this happening with link fields in child docs. (I’m not sure how it’d work either, since child docs can have multiple link fields).

In general, you can do this kind of work with the Fetched From docfield property (accessible via the Customize form tool). Note that this is client side only. If you need to be sure this value is getting set under all circumstances (including API, for example), you’d have to use a server side hook or script.

Additionally, if you’re on v14, you could accomplish this with the new Virtual Docfield:
https://frappeframework.com/docs/v14/user/en/basics/virtual_docfield

@Peterg

Ok that clarifies a lot!

How would I use the Fetched From property to fetch an “item” custom field into a “sale order item” custom field?

I went to Customize Form for “Sale Order Item” and edited the custom field “my_field”. In the “Fetch From” box, I wrote “item.my_field”. I saved the changes.

Then I created a new Sale Order and added an item. After I choose the item_code, the standard fields autopopulate but my custom field does not! :frowning: What is the correct process here?

I think I finally figured it out from finding this Discussion:

In Fetch From, you use the syntax “[source_doctype_primary_index_fieldname].[field_name]”

Personally I think this is way too “magical” looking and non-intuitive for an experienced programmer. Is this even documented anywhere? I’ve spent an immense amount of time trying to figure this out.

Fetch From pulls data from linked documents. In your example, item.my_field didn’t work because there isn’t a link field named item:

The link field is actually called item_code. If you type item_code.my_field, it should work. Is that what you mean by using source_doctype_primary_index.

I can’t think of a more concrete syntax for this field than that, but it any case the whole thing is getting turned into select menus in v14:

https://github.com/frappe/frappe/pull/13760

There’s a bit of documentation here, but it could definitely be improved:
https://frappeframework.com/docs/v13/user/en/guides/app-development/fetch-custom-field-value-from-master-to-all-related-transactions

Wow! Thanks Peter! It all makes sense now

So the correct syntax for the Fetch From property is: [link_field].[field_in_linked_document]

Where “link_field” references the document from which data should be fetched and “field_in_linked_document” is the field in the fetched document.

Thanks for the heads up about the UI change in v14 also! Quite important.

1 Like