function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BB 

How do i link a pdf inserted as a file to a specific opportunity ?

So i call the controller from vf page and the pdf si saved as a file but i dont know how to link it to opportunity 

 PageReference pdf = Page.CreatePDF;
    pdf.getParameters().put('id',opp.Id);
   
    Blob body;
    
    try {
        
        body = pdf.getContent();
        
    } catch (VisualforceException e) {
        body = Blob.valueOf('Error');
    }
 
    ContentVersion cont = new ContentVersion();
    cont.Title = 'lala' +acc.Name + '-' + opp.Number__c + '.pdf';
    cont.PathOnClient = 'lala.pdf';   //The complete path of the document. One of the fields that determines the FileType.
    cont.VersionData = body; // The content or body of the note, which can include properly formatted HTML or plain text. 
    cont.Origin = 'C'; 
    insert cont;    

I dont want it as as attachment
Best Answer chosen by B
Ramesh DRamesh D
B,

//Your PDF
 Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 

//ContentVersion
            ContentVersion contentVersion_1 = new ContentVersion(
                Title='Header_Picture1', 
                PathOnClient ='/Header_Picture1.jpg',
                VersionData = bodyBlob, 
                origin = 'H'
            );
            insert contentVersion_1;
            
            ContentVersion contentVersion_2 = [SELECT Id, Title, ContentDocumentId FROM ContentVersion
             WHERE Id = :contentVersion_1.Id LIMIT 1];
            
            
            ContentDocumentLink contentlink = new ContentDocumentLink();
            contentlink.LinkedEntityId = YourOpportunity.id;
            contentlink.contentdocumentid = contentVersion_2.contentdocumentid;
            contentlink.ShareType = 'V';
            insert contentlink; 


Thanks
Ramesh D

All Answers

JayantJayant
Files are unnecessarily complex while interacting from code.
I guess the record and file are linked using ContentDocumentLink object. You should create one and that would hold the parent record's Id and the ContentDocumentId. So you need to insert 3 records, 1 ContentDocument, 1 ContentVersion and 1 ContentDocumentLink.
Ramesh DRamesh D
B,

//Your PDF
 Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 

//ContentVersion
            ContentVersion contentVersion_1 = new ContentVersion(
                Title='Header_Picture1', 
                PathOnClient ='/Header_Picture1.jpg',
                VersionData = bodyBlob, 
                origin = 'H'
            );
            insert contentVersion_1;
            
            ContentVersion contentVersion_2 = [SELECT Id, Title, ContentDocumentId FROM ContentVersion
             WHERE Id = :contentVersion_1.Id LIMIT 1];
            
            
            ContentDocumentLink contentlink = new ContentDocumentLink();
            contentlink.LinkedEntityId = YourOpportunity.id;
            contentlink.contentdocumentid = contentVersion_2.contentdocumentid;
            contentlink.ShareType = 'V';
            insert contentlink; 


Thanks
Ramesh D
This was selected as the best answer