Create new document on submit

Hi,

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”)
doc.update({
‘opportunity’: opportunity.name,
‘customer_name’: opportunity.customer_name,
‘items’: row.items,
})

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

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:
hook.py:

doc_events = {
    'Opportunity': {
        'on_submit': [
            'shei.events.on_opportunity_on_submit'
        ]
    },
}

events.py:

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

Thank you so much its working! :slightly_smiling_face:

thanka a lot