Frappe.db.sql in server script

Hi every one
I want to write server side script in ERPNext
this script will work on Patient Appointment token_number
I want to get the max value of token_number from database then increment 1

this is my code

numb = frappe.db.sql("""
	select
		Max(token_number)
	from
		`tabPatient Appointment`
	where
	appointment_date=%s and practitioner=%s  and status NOT IN ("Closed", "Cancelled")
	""", (doc.appointment_date,  doc.practitioner))

doc.token_number = numb

when i saved this and try to create anew patient appointment i got Server Error
TypeError: ‘NoneType’ object is not callable

any help please?

Thanks in advance

Share your code and error traceback then someone can help you with this code hard to understand what is your problem.

The function frappe.db.sql() will return a tuple, which contains tuples.

For example, assume the result of your SQL query (the largest token_number) equals 142. The value of variable numb will be:

((142,),)

If you want to assign 142 to object doc.token_number, you must use this syntax instead:

# The first outer tuple, and the first inner tuple
doc.token_number = numb[0][0]

i am getting error even if i don’t make any assignment

You should try running your code in System Console (It’s a Single DocType), you’ll get immediate feedback.

If it is single doctype you try this

numb = frappe.db.sql(“”"
select
Max(token_number)+1
from
tabSingles
where
doctype=%s and appointment_date=%s and practitioner=%s and status NOT IN (“Closed”, “Cancelled”)
“”", (“Patient Appointment”,doc.appointment_date, doc.practitioner))
if numb:
doc.token_number = numb[0][0]
else:
print (“No token number found”)