Passing parameter into controllers before_save in custom class script?

I am using frappe.call method from client side and send object with it. I want this object in before_save defination that was call before the document saved.
When I access this object in before_save controller its show error out of range and not found.

class BrickWiseSale(Document):
def before_save(self, *args, **kwargs):
	# super().save(*args, **kwargs)	
	print(args[0])
@frappe.whitelist(allow_guest=True)

def parse_pdf(pdf_file,parent_detail):
parent_details = json.loads(parent_detail)
dist_city = parent_details[“city”]
# print(doc)
try:
doc = frappe.get_last_doc(‘Brick Wise Sale’,filters={“city”:dist_city});
print(doc)
BrickWiseSale.before_save(BrickWiseSale,doc,parent_details)
except:
pass

Thanks in advance if someone willing to answer me.

You can try to do this in your parse_pdf method:

    doc = frappe.get_last_doc('Brick Wise Sale', filters={"city":dist_city});
    doc.__parent_details = parent_details
    doc.before_save()

And then use self.__parent_details. Anyways i find a lot of weird things in this code. Maybe you want to do it in another method not called “before_save”? It is a hook method, and it will not have your custom parameter when called. Maybe you can do this:

class BrickWiseSale(Document):
    def before_save(self):
        if self.get("__parent_details", ""):
            self.parse_parent_details()

    def parse_parent_details(self):
        # CUSTOM CODE HERE
        print(self.__parent_details)

@frappe.whitelist(allow_guest=True)
def parse_pdf(pdf_file,parent_detail):
    parent_details = json.loads(parent_detail)
    dist_city = parent_details["city"]
    # print(doc)
    try:
        doc = frappe.get_last_doc('Brick Wise Sale',filters={"city":dist_city});
        print(doc)
        doc.__parent_details = parent_details
        doc.parse_parent_details()
    except:
        pass

Or something like that

Thanks for reply.

I just want my object in before_save contorller so that i can set my custom validation.

I did not get print of self._parent_details. Can you help me.

Hi there,

before_save is a hook called by frappe directly as part of a document’s lifecycle. There is no way to pass an object to this method. If you want to use an object passed to a whitelisted method in before_save, you’ll have to save it as a property of the doctype somewhere in the whitelisted method. Then, before_save will be able to access it.

The suggest made by @peterg is a good one. For example, I like to create new objects inside 'flags' in my Documents.

First, I’ll set the flag when it’s appropriate:

customer_doc.flags.run_special_function = True

Then in a controller method like 'before_save', I add a condition:

class Customer(Document):

    def before_save(self):
        if hasattr(self.flags, 'run_special_function') and self.flags.run_special_function is True:
            # additional logic to perform
        ...
1 Like

Thanks brian!
Kindly take a look on code that you suggest. Its not print anything.

    def before_save(self, *args, **kwargs):
    		if hasattr(self.flags, 'run_special_function') and     self.flags.run_special_function is True:
    			print(self.__parent_details)
    			frappe.delete_doc("Brick Wise Sale",self["name"])

whitelist mehtod

    try:
		doc = frappe.get_last_doc('Brick Wise Sale',filters={"city":dist_city});
		doc.__parent_details = parent_details
		doc.flags.run_special_function = True
	except:
		pass

I troubleshoot that problem. When we make object of parent_detail in whitelist defination but it will not access in before_save controller. It’s not set into the doctype.

Hi everyone,

I have added object in document and it will access on whitelist function call but on before save it will show none. what was the reason behind this.

Look at my code.

def before_save(self):
	# if hasattr(self.flags, 'run_special_function') and self.flags.run_special_function is True:
	# print(self.as_dict())
	print(self.get("from"),self.get("__parent_details"))
	if self.get("__parent_details"):
		self.parse_pdf_detail()
def parse_pdf_detail(self):
	print(self.get("__parent_details"))
try:
	doc = frappe.get_last_doc('Brick Wise Sale',filters={"city":dist_city})
	# doc.__parent_details = parent_details
	doc.update({"__parent_details":parent_detail})
	# doc["__parent_details"] = parent_details
	doc.parse_pdf_detail()
	print(doc.get("__parent_details"))
except:
	pass

Console output.
{“city”:“Sheikhupura”,“fromDate”:“2021-12-01”,“toDate”:“2021-12-22”}
10:03:49 web.1 | {“city”:“Sheikhupura”,“fromDate”:“2021-12-01”,“toDate”:“2021-12-22”}
2021-12-01 None