Example of pulling last service date from doctype

I have created a doctype called Service_Notes and would like to show the last service note entered (Service Date) on the customer form. does any one have any examples? on the customer from created a field call last_service_date, link would be customer…

HI @Dan_Powell,

this might not be the easiest solution, but one that should work:

Write a function in your custom doctype (Service Notes) server side code (service_notes.py):

def get_last_service_note(customer):
    sql_query = """SELECT `name` FROM `tabService Notes` WHERE
        `customer` = '{0}' 
        ORDER BY `date` DESC
        LIMIT 1""".format(customer)
    last_service_note = frappe.db.sql(sql_query, as_dict=True)
    if last_service_note:
        return { 'last_service_note': last_service_note[0].name }
        return { 'last_service_note': 'None' }

Add a custom script to the customer doctype, which on refresh loads the last service date

frappe.ui.form.on('Customer', {
  refresh: function(frm) {
      method: 'myapp.myapp.doctype.service_notes.service_notes.get_last_service_note',
      args: {
        'customer': frm.doc.name
      callback: function(r) {
	if (r.message) {
	  frm.set_value('last_service_note', r.message.last_service_note[0]);

Hope this helps.

Thank you, this was very helpful.