How to make a generated File selected by default in the manual email wizard?

Hello,

I am customizing account.invoice in Tryton and I generate an XML file as a binary field:

invoice_xrechnung

invoice_xrechnung_filename

My problem is that when I open the email wizard, so the XRechnung is not automatically selected in the attachements field. at now after generate the file i can find it in attachements directly but not selected automatically , when I open the email composer and that what I want is to be selected automatically without let me everytime select the file when I want to send email

again for more clarity, What I want is:

when I open the email compose window from an invoice,

the generated XML File should appear selected by default in the attachments or files area,

without the user needing to choose it manually in attachements.

I would like to know How i can make generated file to be by default selected on attachements field without select it by my self  

This is my code now:

class Invoice(metaclass=PoolMeta):
name = ‘account.invoice’
invoice_xrechnung = fields.Binary(‘XRechnung’,
filename=‘invoice_xrechnung_filename’)
invoice_xrechnung_filename = fields.Function(fields.Char(
‘XRechnung filename’), ‘get_invoice_xrechnung_filename’)


def generate_xrechnung(self):
    pool = Pool()
    Configuration = pool.get('account.configuration')
    Invoice = pool.get('account.invoice')

    config = Configuration(1)
    transaction = Transaction()

    if self.invoice_xrechnung:
        self.attach_xrechnung()  


def attach_xrechnung(self):
    if self.invoice_xrechnung:
        Attachment = Pool().get('ir.attachment')
        existing = Attachment.search([
            ('resource', '=', '%s,%s' % (self.__name__, self.id)),
            ('name', '=', self.invoice_xrechnung_filename),
        ])
        if existing:
            print(f"[DEBUG] Attachment already exists for invoice {self.rec_name}")
        else:
            Attachment.create([{
                'name': self.invoice_xrechnung_filename,
                'data': self.invoice_xrechnung,
                'resource': '%s,%s' % (self.__name__, self.id),
                'type': 'data',  
            }])
            print(f"[DEBUG] Attachment created: {self.invoice_xrechnung_filename}")
         

Is it feasible to do that??

You would need to define an email template for the invoice and select the corresponding report.

Thank you Mr.Cédric for your response. I understand the email template solution works when the attachment is a report output(odt/fodt), but my XRechnung is a generated XML binary field on account.invoice, not a report so it will not work in my case

Perhaps you could override the _execute method of a new report.

Here is some code Sergi authored for me for a very similar issue:

class InvoiceReport(Report):
    __name__ = 'account.invoice.anaf'

    @classmethod
    def _execute(cls, records, header, data, action):
        record = records[0]
        return 'xml', record.get_payload()

In this case the report returns the record.get_payload(). So you could return the xreichnung binary field.

Thanks Mr.Josef, I will try this