Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
6094 Visualizzazioni

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
Abbandona
Risposta migliore

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
Abbandona

<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>

Post correlati Risposte Visualizzazioni Attività
1
nov 22
2776
2
ago 22
7075
1
gen 25
8675
1
mar 21
4622
1
gen 21
1779