I’ve made a goals table that contains goals and whether it is completed or not. and I want to calculate the total percentage of goals that have been completed and fill that field with server-side calculation after the user save that form. Visual representation attached below:
class GoalDoc(Document):
def validate(self):
count = 0
total = 0
for i in self.goals:
if True:
total = total + 1
if i.completed:
count = count + 1
perc = (count/total) * 100
get_check_count(perc)
@frappe.whitelist()
def get_check_count(perc):
return perc
This code doesn’t really make sense. Your method get_check_count doesn’t do anything except return the value sent to it. I don’t really understand why you’re calling it from both your validation method and your client script.
Also, you definitely don’t want to be calling this hook after save (nor in validation). You want to do it before save. Something like this:
class GoalDoc(document):
def before_save(self):
count = 0
for i in self.goals:
if i.completed:
count = count + 1
self.perc = count / len(self.goals) * 100