I've button when i click on it it generates new file , i want to attach that file to email The problem i'm facing is that i found duplicate attachments when i click on button twice , 3 attached file when i click on button 3rd time .. ..
Code
public void sendPDF() {
PageReference pdf = Page.CRequest2;
Attachment attach = new Attachment();
Blob body;
try{
if(!test.isRunningTest()) body = pdf.getContentAsPDF();
if(test.isRunningTest()) body = Blob.valueof('Some random String');
system.debug('got the body');
ContentVersion contVer = new ContentVersion(Title='Goldstar Request '+this.catrequest.Account__r.Name + ' ' + this.catreq.Name , VersionData = EncodingUtil.base64Decode('Unit Test Attachment Body'), PathOnClient ='/Catalog.pdf');
List<ContentDocumentLink> docLinks = [
SELECT Id, ContentDocumentId
FROM ContentDocumentLink
WHERE LinkedEntityId = :this.catrequest.id
];
// check if file already exists,
// if yes, insert a newer ContentVersion by specifying the ContentDocumentId
if (!docLinks.isEmpty()) {
contVer.ContentDocumentId = docLinks[0].ContentDocumentId;
}
insert(contVer);
// if file didn't exists before
// insert a new ContentDocumentLink
if (docLinks.isEmpty()) {
ContentDocumentLink contentlink = new ContentDocumentLink();
contentlink.LinkedEntityId = this.catrequest.Id;
contentlink.ContentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:contVer.Id].ContentDocumentId;
contentlink.ShareType = 'V';
insert contentlink;
}
//Now email the catalog Request
String currentUserEmail = UserInfo.getUserEmail();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{currentUserEmail,'SamplesGS@goldstar-europe.com'};
mail.setToAddresses(toAddresses);
// String plainBody = 'https://goldstar1.lightning.force.com/'+this.catrequest.id;
mail.setPlainTextBody('');
// string formattedDate = this.catrequest.Request_Date__c.day() +'/'+this.catrequest.Request_Date__c.month() +'/'+this.catrequest.Request_Date__c.year();
String subject = 'Goldstar Request : ' +this.catrequest.account__r.BillingCountry +', ' + this.catrequest.Requested_By_Internal__r.FirstName +' '+ this.catrequest.Requested_By_Internal__r.LastName +', Distributor : '+ this.catrequest.account__r.name+' '+this.catrequest.Name ;
mail.setSubject(subject);
List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>();
if(contVer != null){
Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
attachment.setBody(contVer.VersionData);
attachment.setContentType('application/pdf');
attachment.setFileName(contVer.Title+'.pdf');
}
//query for any files attached to the catalog request and include them in the email
set<id> docIds = new set<id>();
list<contentdocumentlink> links = [Select Id,contentdocument.id from ContentDocumentLink where LinkedEntityId = :this.catrequest.id];
For(contentdocumentlink l: links){
docIds.add(l.contentdocument.id);
}
list<contentversion> versions = [SELECT ContentDocumentId,VersionData,FileExtension,FileType,Id,Title FROM ContentVersion where ContentDocumentId IN: docids];
for(contentversion a: versions){
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Title+'.'+a.FileExtension);
efa.setBody(a.VersionData);
attachments.add(efa);
}
if(!attachments.isEmpty()) mail.setFileAttachments(attachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
this.catrequest.Request_Last_Sent__c = system.now();
update this.catrequest;
} catch (Exception e) {
status = e.getMessage();
system.debug('error ' + e.getMessage());
}
another thing : when i upload file manualy and try to generate new file i couldn't file the file i've upload as attachement ..