Get last document using filter not name

I need to fetch the last purchase invoice for the created purchase invoice, using the Item Code in the purchase invoice

I try this code but it’s not working

rate = frappe.db.get_value('Purchase Invoice Item', {'item_code': ("=", 'TEST_DEMO')}, ['rate'])

can any one help me?

you can use this

rate = frappe.db.get_all('Purchase Invoice Item', filters={'item_code': 'TEST_DEMO'}, fields=['rate'], limit=1, order_by='creation desc')
if rate:
    item_rate = rate[0]['rate']
    item_rate = 0  # Provide a default value if the record is not found

@Omar_Mohammed try this :
rate=frappe.db.sql("Select rate from ``tabPuchase Invoice Item`` where item_code=%(item)s order by creation limit 1 " ,values={"item":"TEST_DEMO"})[0]

1 Like

I tried the code but didn’t return anything :thinking: