Fetch field values from child table of current parent doctype


I’m trying to create a print format for sales invoice. For this purpose, I need to show some values of a child table.

Let me explain my use case in detail. I have a child table “Vehicle Data” in Customer DocType. For each customer, I will add details of their vehicle. Now when I print the sales invoice I need the 3 fields from vehicle details corresponding to the customer in the invoice.

What I did now is I used custom html block and used jinja template inside the html block.
This is the code that I used to fetch vehicle details: frappe.db.get_value(“Vehicle Data”, doc.customer, vehicle_number ). This gives me the value. But what I noticed is that whenever I change the Vehicle details in any other customer document or create a new customer with new vehicle details, and refresh my older sales invoice, the new vehicle details are loaded into the form.

How do I only select the vehicle details corresponding to the customer I have chosen in the invoice?

@raheeb hi, i think it is better to fetch vehicle details in sales invoice while selecting customer and save vehicle details in sales invoice also . then select vehicle details from sales invoice to create print format

You mean create a new table of vehicle in sales invoice as well?

yes what i meant

I solved it using jinja templating.

Example field:

Vehicle Number

{% set customer = frappe.get_doc(“Customer”, doc.customer) %}
{%- for row in customer.vehicle -%}
{{ row.vehicle_number }}
{%- endfor -%}

Sir I try this code but not working