Copy Item Attribute to Custom Fields on other documents

As far as I’m aware, there is no way to use the fetch_from DocType feature to obtain deep nested values like Item Attributes.

So, in this example we copy the Colour Item Attribute Value to a custom colour field on the BOM using a custom script set to:

  • Script Type: DocType Event
  • Reference Document Type: BOM
  • DocType Event: Before Validate

Code:

if doc.item:
        item = frappe.get_cached_doc('Item', doc.item)
        for row in item.attributes:
            if row.attribute == 'Colour' and row.attribute_value:
                doc.colour = row.attribute_value.strip()