Create new document on submit


I need to create a new document called Sourcig Request which is a custom doctype on submitting Opportunity. I need it to be created only using server side script. I tried the below but its not working,

def create_sourcing_request(opportunity, row, auto_create=False):
doc = frappe.new_doc(“Sourcing Request”)
‘customer_name’: opportunity.customer_name,
‘items’: row.items,

if auto_create:
	doc.flags.ignore_mandatory = True
	frappe.msgprint(_("Sourcing Request {0} created").format(

return doc

I am not sure whether this is right and also not sure where to call this function , need help.

Thanks in advance!

You need to create a hook on Opportunity on_submit,
then when it calls your methods, you can create the new file as you want. Here’s a little sample:

doc_events = {
    'Opportunity': {
        'on_submit': [

def on_opportunity_on_submit(doc, handler=""):
    sourcing_request = frappe.new_doc('Sourcing Request')
    sourcing_request.flags.ignore_permissions  = True
    sourcing_request.update({'opportunity', 'customer_name':doc.customer_name, 'items':doc.items}).save()

Thank you so much its working! :slightly_smiling_face:

thanka a lot