Creating and attached PDF file in Document modules

Hello Everyone,

I created a custom generate PDF file in the Opportunity module, and it will send an email with the Generated PDF file and it will automatically attached to it. Now my concern is that I want to create a record in the Document Module and relate it to the Opportunity Module. I can auto-create a record and relate it to the Opportunity Module, but I don`t know how I am going to attach the Generate PDF file to the Document Module.

Hope you guys were able to understand what I want to accomplished.

Regards

It’s simpler to do with a Note (containing an attachment), rather than a Document which has an additional record in between (a Revision).

You can learn about how to do it here:

Code to create Note with attachment can look something like this example I took from one of my projects (you’ll need to adapt it, it’s connecting to a parent Case, not Opportunity):


$attachmentFilePath = dirname($originPath) . '/' . $attachment['fs_filename'];
$noteBean = \BeanFactory::newBean('Notes');
$noteBean->id = create_guid();
$noteBean->new_with_id = true;
$noteBean->name = $jsonMail['protocol_number'] . ' - ' . $attachment['crm_filename'];
$noteBean->file_mime_type = mime_content_type($attachmentFilePath);
$noteBean->filename = $attachment['crm_filename'];
$noteBean->parent_type = 'Cases';
$noteBean->parent_id = $caseBean->id;
copy($attachmentFilePath, $this->getProjectDir() . '/public/legacy/upload/'. $noteBean->id);
$noteBean->save();

That public/legacy part is for v8, drop it if you’re still using v7.

Hi @pgr , noted on this and will update you once I start working on my project. I appreciate your help!