I have a cron job and email template and I am trying to write a python code to send acknowledgement emails to new applicants once the application is submitted by applicants and hit the first stage of Recruitment in Odoo 14. How can I access the applicant that has just applied and trigger the cron job to send the email to the applicant in minutes? Any help will be highly appreciated. Here's my code.
Py.py file:
def job_app_acknowledgments(self):
for rec in self:
rec.partner_id = self.env["hr.applicant"].search(
[("job_id", "=", job.id), ("stage_id", "=", job._get_first_stage().id)]
)
if rec.partner_id:
ctx = {}
ctx['email_to'] = rec.partner_id.email
# ctx['email_from'] = self.env.user.user_id.email
ctx['email_from'] = self.env.user.employee_id.work_email
ctx['send_email'] = True
ctx['partner_id'] = rec.partner_id.id
template = self.env.ref('hr_recruitment_new.job_applicant_acknowledgment')
template.with_context(ctx).send_mail(
rec.id, force_send=True, raise_exception=False)
Cron.xml
<record id="job_applicant_acknowledgment" model="ir.cron">
<field name="name">Job Applicant Acknowledgment Email</field>
<field name="model_id" ref="hr_recruitment.model_hr_applicant"/>
<field name="state">code</field>
<field name="code">model.job_app_acknowledgments()</field>
<field name="user_id" ref="base.user_root"/>
<field name='interval_number'>1</field>
<field name='interval_type'>minutes</field>
<field name="numbercall">-1</field>
<field name="doall" eval="False"/>
</record>
I found the answer: email_sent is a boolean field. The cron job is fine then add your email template.
def job_app_acknowledgments(self):
for rec in self:
if rec.partner_id and not email_sent:
ctx = {}
ctx['email_to'] = rec.partner_id.email
# ctx['email_from'] = self.env.user.user_id.email
ctx['email_from'] = self.env.user.employee_id.work_email
ctx['send_email'] = True
ctx['partner_id'] = rec.partner_id.id
template = self.env.ref('hr_recruitment_new.job_applicant_acknowledgment')
template.with_context(ctx).send_mail(
rec.id, force_send=True, raise_exception=False)