1

Good day, I have a binary field that holds an xml file which i generate with etree.Element

batchfile = fields.Binary("XML Batch File", readonly=True)

So i want to attach this Binary file so that I email it, but how can I do this?

    def send_claim_batch(self):
    self.ensure_one()
    ir_model_data = self.env['ir.model.data']
    try:
       template_id = ir_model_data._xmlid_lookup('iemr_medical_aid.iemr_claim_batch_email')[2]
    except ValueError:
       template_id = False
       
    try:
       compose_form_id = ir_model_data._xmlid_lookup('mail.email_compose_message_wizard_form')[2]
    except ValueError:
        compose_form_id = False
    
    template_id = self.env.ref('event.event_registration_mail_template_badge')
    compose_form = self.env.ref('mail.email_compose_message_wizard_form')
    
    ctx = {
       default_model: 'iemr.claim.batch',
       default_res_id: self.ids[0],
       default_use_template: bool(template_id),
       default_template_id: template_id,
       default_composition_mode: 'comment',
       mark_so_as_sent: True,
       force_email: True,
    }
    return {
       'type': 'ir.actions.act_window',
      'view_type': 'form',
       'view_mode': 'form',
       'res_model': 'mail.compose.message',
       'views': [(compose_form.id, 'form')],
       'view_id': compose_form.id,
       'target': 'new',
       'context': ctx,
    }
Abraham Kalungi
  • 45
  • 1
  • 10

1 Answers1

1

I cant test it but i think it is something like that.

ctx = {
   default_model: 'iemr.claim.batch',
   default_res_id: self.ids[0],
   default_use_template: bool(template_id),
   default_template_id: template_id,
   default_composition_mode: 'comment',
   mark_so_as_sent: True,
   force_email: True,
}
if self.batchfile:
    attachment = self.env['ir.attachment'].create({
        'name': "%s.xml" % self.id,
        'datas': self.batchfile,
        'type': 'binary',
        'res_model': self._name,
        'res_id': self.id,
    })
    ctx['default_attachment_ids'] = [attachment.id]
return {
     'type': 'ir.actions.act_window',
     'view_type': 'form',
     'view_mode': 'form',
#...
Paxmees
  • 1,540
  • 1
  • 15
  • 28
  • Thank you @Paxmees attachment = self.env['ir.attachment'].create({ 'name': "%s.xml" % self.id, 'datas': self.batchfile, 'type': 'binary', 'res_model': self._name, 'res_id': self.id, }) context = { 'default_attachment_ids': attachment.id, } I get an error – Abraham Kalungi Jul 07 '22 at 09:32
  • 1
    what is the error you are getting – Paxmees Jul 14 '22 at 16:52
  • The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Odoo\15\server\odoo\http.py", line 644, in _handle_exception return super(JsonRequest, self)._handle_exception(exception) File "C:\Odoo\15\server\odoo\http.py", line 302, in _handle_exception raise exception.with_traceback(None) from new_cause ValueError: Wrong value for mail.compose.message.attachment_ids: 432 – Abraham Kalungi Jul 15 '22 at 16:13