Skip to Content
Menu
This question has been flagged
1 Reply
5019 Views

Hello 
I have to send email when custom_date_from - today date = 7 then send this email template->
leave_email_template
and if custom_date_from - today =1 send this email template->
leave_request_email_template

Here I have written method for this but i donot know where I am wrong emails are not sending





@api.model
def test_cron_job(self):

print("abcddddddddddddddddddddddddddddddddddddddddddd")
today = fields.Date.today()
custom_date = self.env['hr.holidays'].search([('custom_date_from', '>=', today)])
print("custom_date", custom_date)
for rec in custom_date:
if fields.Date.from_string(rec.custom_date_from) - fields.Date.from_string(today) == 7:
mail = self.env['mail.template'].browse([('half_day_leave.leave_email_template')])
mail.send()
else:
if fields.Date.from_string(rec.custom_date_from) - fields.Date.from_string(today) == 1:
mail = self.env['mail.template'].browse([('half_day_leave.leave_request_email_template')])
mail.send()
Please Help me to solve this 

Thanks

Avatar
Discard
Best Answer

Hi,

browse method take id of the template so you can write your code as below:

@api.model
def test_cron_job(self):
print("abcddddddddddddddddddddddddddddddddddddddddddd")
today = fields.Date.today()
custom_date = self.env['hr.holidays'].search([('custom_date_from', '>=', today)])
print("custom_date", custom_date)
for rec in custom_date:
if fields.Date.from_string(rec.custom_date_from) - fields.Date.from_string(today) == 7:
mail_template = self.env['mail.template'].browse(self.env.ref('half_day_leave.leave_email_template').id)
mail_template.send_mail(rec.id)
else:
if fields.Date.from_string(rec.custom_date_from) - fields.Date.from_string(today) == 1:
mail_template = self.env['mail.template'].browse(self.env.ref('half_day_leave.leave_email_template').id)
mail_template.send_mail(rec.id)

If you want to call this method from Automic Action (Cron Job) then you will create cron job xml and call method.

Avatar
Discard

<odoo noupdate="1">
<record id="hr_holiday_half_day_cron" model="ir.cron">
<field name="name">Half Day Send Email</field>
<field name="model_id" ref="hr_holidays.model_hr_holidays"/>
<field name="state">code</field>
<field name="code">model.test_cron_job()</field>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="doall" eval="False"/>
</record>
</odoo>

Related Posts Replies Views Activity
1
Nov 22
1752
2
Aug 22
5993
1
Nov 21
7418
1
Mar 21
3630
1
Jan 21
1082