Print Format - how to get value from another document?

i am coming up with an issue that has been asked several times, unfortunatelly no answer could help in my case.
I am creating Invoices, Orders and Delivery Notes with the Print Format. I am not really familiar with frappe that’s why I am having a hard time using frappe.db.get_value("[doctype]", “[name]”, “fieldname”) to get a value from another Document.

For example, our Customers have their own Item codes which have to be shown in invoices.
This Code is entered in the referring Item at Costumer Ref. Code.
How do i get this code in the Sales Invoice?

Also our Items have two different Item Names (Item Name 1 and Item Name 2). I have created a field for the Item Name 2 in the Purchase Order Item Table aswell. When I am selecting the Item , Item Name 2 field will still be empty.
How do I get Item Name 2 in the Purchase Order aswell as in the Purchase Order Print Format.

I have tried different version of the code which i mentioned above but it didn’t work.

Thank you very much for your help!

Can you share your code snippet?

Hi thank you for responding,

this is how i tried it for the Item Name 2… But it is not working… let me know if you need more to see than that!

would the solution be the same for the Customer Reference Code?

Thank you

What is item_name_2?

Use row.item_code instead of doc.item

Thank you very much!
I tried row.item_code and it is working! thanks for that.

I still have the same problem with Customer Reference Code. I tried to adopt your solution for the ref_code but I could not figure out which row. or doc. is required in this case. What do I have to do?

Thank you so much for your help!