You need to sign in to do that
Don't have an account?

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
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
//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
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.
//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