Email To Suppliers one day before requested date of Purchase Order

I would like to send email to suppliers one day before request.

Suppose 3 purchase orders requested date is same. In this case emails dont go all the three suppliers. All three email has been sent to same Supplier (Whose purchase order has been created firs).

How can i work it to all 3 suppliers seperately ?

Thanks

Hey ,
This is demo eg for sending mail to the supplier . Since email is not found directly on supplier for ,you can fetch mail id from address form

Email Sending on the basis of roles

def custom_validate(doc,method):
notify_to_approver(doc,method)

def notify_to_approver(doc,method):
if doc.supplier:
message = “Purchase Order : {name}”.format(name=doc.custom_naming)+"
"
message += “Schedule Date :
{from_date}”.format(from_date=frappe.utils.formatdate(doc.schedule_date))

parent = frappe.db.get_value(“Dynamic Link”,{“link_name”:doc.supplier},“parent”)

email_to = frappe.db.get_value(“Address”,{“name”:parent,“address_type”:‘Billing’},“email_id”)
if email_to:
email_md = email_to
send_mail_custom(doc,email_md,message)

def send_mail_custom(doc,recipients,message):
subject=“Purchase order”+ doc.name
frappe.sendmail(
recipients=recipients,
sender=frappe.session.user,
subject=subject,
delayed=False,
template=“new_message”,
args={
“from”: “Epenal Group of Companies”,
“message”: message,
“link”: get_url()
},
header=[(‘New Message’),‘orange’])

2 Likes

Thanks mam,I try this out

I have pasted this code on api.py

Can you tell me how do i make a call to this function ? If from custom script,can you send me that code.

How can i replace that arguments (doc and method) ?

Yes you can call method custom_validate of py side via frappe.call from js side

Syntax:
frappe.call({
method:"…",
args:{
doc:cur_frm.doc
}
callback:function®{
//condition
}
})

1 Like

def custom_validate(doc,method):

how can i replace above line(arguments) for my requirement ?

You can do one thing like whatever the field you require for doing operation on py side, you send inside the args of frappe.call
For eg

in JS file
args:{
doc : cur_frm.doc ,
supplier : cur_frm.doc.supplier
}

In PY file

def method_name(doc,supplier)

//and perticular operations

1 Like

Hey,

I guess there is some problem in method : send_mail_custom,I debuged untill this method,

Js code:

frappe.ui.form.on(“Purchase Receipt”, “on_submit”, function(frm, cdt, cdn) {
var d = frappe.get_doc(cdt, cdn);
var supplier_email = d.contact_email ;
send_email_to_supplier( supplier_email );
}); //End_of_frappe_call

function send_email_to_supplier(supplier_email) {
frappe.call({
method: “nhance.api.check”,
args : {
“supplier_email_id” : supplier_email
},
async: false,
callback: function® { }
});

}

My PY code :

@frappe.whitelist()
def check(supplier_email_id):
if supplier_email_id:
notify_to_approver( supplier_email_id )

def notify_to_approver(supplier_email_id):

email_to = supplier_email_id
send_mail_custom(email_to)

def send_mail_custom(recipients):

subject=“Purchase order Checking via manual code”
frappe.sendmail(
recipients=recipients,
sender=frappe.session.user,
subject=subject,
delayed=False,
template=“new_message”,
args={
“from”: “Epenal Group of Companies”,
“message”: "Hey success",
“link”: get_url()
},
header=[(‘New Message’),‘orange’])

I have attached the error what i am getting

This error is only because of the data or resource you send from js to py is not avalaible

In frappe.call of js , check the proper path which you give under the method through bench console

1 Like

I’ve checked,

i am getting resource from js ,

I am getting that error ,while inserting the below code, in py.untill then evrything works fine.

Can you any error in the below code?

def send_mail_custom(recipients):

frappe.sendmail(
    recipients=recipients,
    sender=frappe.session.user,
    subject="PO checking",
    delayed=False,
    template=“new_message”,
    args={
    “from”: “Epenal Group of Companies”,
    “message”: "Hey success",
    “link”: get_url()
},
header=[(‘New Message’),‘orange’])

Whats the error?

The resource you are looking for is not available

You can try this one, it will on validate functinality

JS Code : eg purchase_receipt.js

frappe.ui.form.on(“Purchase Receipt”,
validate:function(frm) {
frappe.call({
method: “app_name.app_name.purchase_receipt.purchase_receipt.notify_to_approver”,
args : {
“doc” : cur_frm.doc
}
});
}

PY code :Eg purchase_receipt.py

@frappe.whitelist()
def notify_to_approver(doc):
if doc.get(“supplier”):
parent = frappe.db.get_value(“Dynamic Link”,{“link_name”:doc.supplier},“parent”)
email_to = frappe.db.get_value(“Address”,{“name”:parent,“address_type”:‘Billing’},“email_id”)

send_mail_custom(email_to)

def send_mail_custom(recipients):
subject=“Purchase Receipt Checking via manual code”
frappe.sendmail(
recipients=recipients,
sender=frappe.session.user,
subject=subject,
delayed=False,
template=“new_message”,
args={
“from”: “Company Name”,
“message”: “Hey success”,
“link”: get_url()
},
header=[(‘New Message’),‘orange’])

1 Like

Hello,

I guess the the problem is in this method "def send_mail_custom(recipients): "

I am getting email_id from previous function . I’ve checked it,

While inserting “send_mail_custom” function that error the resource not available is repeating.

I have tried by giving static email id in recepients also, still throwing same error,

Did that work perfect for you. you can able to send mail using this code?

Yes , this worked for me. The only thing you need to check on both js and py side , is what you sending and what you are getting . Just check it by printing on both side. Or try to inspect the page and show the error

1 Like

Hey pooja,

I could not fetch supplier name from doc object which you have suggested in your last code,

So i am fetching supplier name directly and i checked in console and server I am getting that supplier name and email_id as well.

And i can get email_id in “send_mail_custom” function also,

it works fine before frappe call which is inside “send_mail_custom” function,

It giving this error

I am getting Supplier name_i have checked
Js code:

frappe.ui.form.on("Purchase Receipt", "validate", function(frm, cdt, cdn) {
	console.log("hello") ;
    	var d = frappe.get_doc(cdt, cdn);
	var supplier_address = d.supplier_address ;
	console.log(supplier_address) ;
	frappe.call({
	method: "nhance.api.notify_to_approver",
	args : {
		"name" : supplier_address
	}
	});
});

Python code:

@frappe.whitelist()
def notify_to_approver(name):
    address_record = frappe.get_doc("Address",name)
    email_to =  address_record.email_id
    send_mail_custom(email_to)

def send_mail_custom(recipients):
    subject=“Purchase Receipt Checking via manual code”
    frappe.sendmail(
    recipients=recipients,
    sender=frappe.session.user,
    subject=subject,
    delayed=False,
    template=“new_message”,
    args={
    “from”: “Company Name”,
    “message”: “Hey success”,
    “link”: get_url()
    },
    header=[(‘New Message’),‘orange’])

Hey ,
I use same code for purchase receipt form . The only difference is I gave the path of py method in frappe call method ie app_name.app_name.purchase_receipt.purchase_receipt.notify_approver and same thing work for me

You have not set email alert from Email alert doc right?

You are only using this custom code to send email right ?

Yes…

1 Like