Update a server script

Hello, I have an old script to use for sending email via quotation form when I click on button. It’s possible to simplified this one to only have it on server and client script ?

Client script :

frappe.ui.form.on('Quotation', {
	refresh: function(frm){
	   frm.add_custom_button(__("Send Email"), function() {
	       if (frm.doc.contact_email){
        	         frappe.call({
            			method: "mm_app.script.send_email",
            			args: {
            			    doctype: frm.doc.doctype,
            			    docname: frm.doc.name
            			}
            		})
	       }
    	   else{
    	        frappe.msgprint("There is no email found for Customer")
    	    }
    	})
    }
})

Python file :


import frappe
from frappe import _

@frappe.whitelist()
def send_email(doctype, docname):
	parent_doc = frappe.get_doc(str(doctype), str(docname))
	args = parent_doc.as_dict()
	
	#email_template = frappe.get_doc("Email Template", parent_doc.custom_email_template)
	
	if not parent_doc.custom_email_text:
		email_template = frappe.get_doc("Email Template", "Quotation")
		parent_doc.custom_email_text = email_template.response_html
		parent_doc.custom_email_subject = email_template.subject

	email_subject = parent_doc.custom_email_subject
	
	message = frappe.render_template(parent_doc.custom_email_text, args)
	
	if parent_doc.custom_email_text:
		frappe.sendmail(
			recipients=str(parent_doc.contact_email),
			subject= email_subject,
			message= message,
			reference_doctype= doctype,
			reference_name= docname,
			#attachments=my_attachments
		)
		frappe.msgprint(_("Email sent"))

Hi @lateliercom,

Your code is already made simple and correct, so no need to change anything in the code. However, if you need to add something to this code, you can try using your own approach. You can’t develop something using only the client side or the server side separately. But if you want to make it so that when a quotation is submitted, an email is automatically sent, you can achieve this using only server-side scripting. Alternatively, you can also use the Notification feature in the system.

Thank You!

HI @NCP
Thanks for the reply, how can I merge both for having work only in server side script ?

Hi @lateliercom,

If you prefer to handle things only on the server side, utilize document events and apply the on_submit method to the document type. You can implement code scenarios such as triggering an email when a quotation is submitted. This is the only method to manage it.

If you want a similar scenario without any custom logic, you can use the notification document type.

However, you cannot manage both scripts using a single approach.

Thank You!

@NCP Thanks for you’r reply. What I want to do, it’s to have a button “Send email” in quotation form, and when we click on it, it’s send an email template (allready create) with the PDF attached.

Any idea how can I do it ?

okay @lateliercom,

You’ve already created the code and shared it in your initial post, so what seems to be the issue? Your code appears to be fine to me.

When I put the python file in the server side script, I have an error and it’s not working. I have the two first lines sending error.

Hi @lateliercom,

If you want to add your scenario then you have a create a custom app and then set it hook.py file and call it.

Please check the references:

Also check the hook concept.

I hope this helps.

Thank You!

1 Like

Hi @NCP

Thanks for the reply, I created an app, but I don’t know where to put my py file ? In which directory I need to put it ?

Hi @lateliercom,

Please check it.

My suggestion that you can put the py file in the overrides folder but first, you have to create an overrides folder if not there.

Thank You!

Thanks @NCP I finaly did it, but how can also put the client script in the app and not in the ERPnext admin section ?

Hi @lateliercom,

Please check the hook concept.

Thank You!