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
Mahesh KGMahesh KG 

How to save the attachment in pdf for an account

Hi Team,

Can you please help me saving the pdf document in Notes & Attachments section of the Account creation.

i am able to save the file but not as an pdf attachment. when i had set contenttype as application/pdf. the file is not getting opened.

Thanks and Regards
Mahesh 
 
Mahesh KGMahesh KG
please find the issue that i am getting when opening the file,

User-added image
Foram Rana RForam Rana R
Hi Mahesh,

Hope you are doing well...!!

Please try below code :
in which I have declared one blob variable name filebody .
You need to assign you blob file body to filebody variable.
 
public class PDF_Class {

    public void insertpdf() {
        
        Contact a = new Contact();
        a.lastname = 'test-foram3';
        insert a;
        
        blob filebody; //set your filebody here
        String fname = 'Resume.pdf';
        ContentVersion conVer = new ContentVersion();
        conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
        conVer.PathOnClient = fname; // The files name, extension is very important here which will help the file in preview.
        conVer.Title = fname; // Display name of the files
        conVer.VersionData = filebody; // converting your binary string to Blog
        insert conVer;  
        
        // First get the content document Id from ContentVersion
        Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
        
        //Create ContentDocumentLink
        ContentDocumentLink cDe = new ContentDocumentLink();
        cDe.ContentDocumentId = conDoc;
        cDe.LinkedEntityId = a.Id; // you can use objectId,GroupId etc
        cDe.ShareType = 'V'; // Inferred permission, checkout description of ContentDocumentLink object for more details
        cDe.Visibility = 'AllUsers';
        insert cDe;
        
        
        
        
        

    }
    
}
Regards,
Foram Rana
 
Mahesh KGMahesh KG
Hi Rana,

I tried the below code,
when i am opening the file i am not able to load it,

 Attachment attach = new Attachment();
                                       attach.ParentId=restAcc.Id;
                                       attach.ContentType=contentType;
                                       attach.Name=attachName;
                                       Blob beforeblob = Blob.valueOf(euaBody);
                                       String paramvalue = EncodingUtil.base64Encode(beforeblob);
                                       System.debug(euaBody + ' is now encoded as: ' + paramvalue);
                                       Blob afterblob = EncodingUtil.base64Decode(paramvalue);
                                       System.debug(paramvalue + 'is now decoded as: ' + afterblob.toString());
                                       attach.Body=afterblob;
                                       //attach.Body=EncodingUtil.base64Decode(euaBody);
                                       insert attach;

Can you suggest me what changes needs to be done?


Thanks and Regards
Mahesh KG
Foram Rana RForam Rana R

If you are uploading pdf then contentType must be application/pdf
I just want to know what is the type of euaBody ?? 
euaBody is encoded base 64 String?
What is the size of file body ??
Mahesh KGMahesh KG
yes, the contentType is application/pdf
the type of the euaBody is String
file size is 6mb
Mahesh KGMahesh KG
Hi Team,

Any update on this?

any help will be appreciated, Thanks
Mahesh KGMahesh KG
Any update on this?
 
Mahesh KGMahesh KG
Is there Solution for this problem? if so please reply and help us with that.