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
Heather_HansonHeather_Hanson 

Apex Class - SavePDF to Files related list

Hello,

I'm a novice at Apex code and am trying to adjust my extension for SavePDF so that it saves as a file as well.  I get an error message related to the line that says:  file.Origin = 'a';

I am indicating the wrong origin or something?  Or do I need to complete the attachment fully before I can add the file?  Any help would be appreciated!

 
public pageReference savePDF(){
    
        Date dateToday = Date.today();
        String sMonth = String.valueof(dateToday.month());
        String sDay = String.valueof(dateToday.day());
        if(sMonth.length()==1){
          sMonth = '0' + sMonth;
        }
        if(sDay.length()==1){
          sDay = '0' + sDay;
        }
        String sToday = String.valueof(dateToday.year())+ '-' + sMonth +  '-' + sDay ;
        
      PageReference pagePdf = new PageReference('/apex/SalesAgreement_PDF');
          pagePdf.getParameters().put('id', idVal);
          Blob pdfPageBlob;
          pdfPageBlob = pagePdf.getContentAsPDF();
                 
          Attachment a = new Attachment();
          a.Body = pdfPageBlob;
          a.ParentID = idVal;
          a.Name = 'Service_Agreement_' + strOppName + '_' + sToday + '.pdf';
          a.Description = 'Service Agreement_' + sToday;
          insert a;

          ContentVersion file = new ContentVersion();
		  file.Title = 'Service_Agreement_' + strOppName + '_' + sToday + '.pdf';
		  file.PathOnClient = 'Service_Agreement_' + Datetime.now().getTime() + '.txt';
		  file.VersionData = Blob.valueOf('Service_Agreement_' + 
		  Datetime.now().getTime() + '.txt');
		  file.Origin = 'a';
		  insert file;         
    
        
        pop_msg='Successfully saved';
        displayPopup = true;  
       
        
        opp.Signature_Date__c = null;
        opp.Signature_Key__c = '';
        opp.Signature_Name__c = '';
        opp.Signature_URL__c = null;
        opp.Signature__c = null;

        
        update opp;
      return new PageReference('/apex/SalesAgreement_HTML?id=' + idVal);
      
        
    }



 
Best Answer chosen by Heather_Hanson
Heather_HansonHeather_Hanson
I figured this out and got it working.  I already had an extension created that does SavePDF as an attachment so I just added the following to have it also save as a File:

 
ContentVersion file = new ContentVersion();
		  file.Title = 'Service_Agreement_' + strOppName + '_' + sToday + '.pdf';
		  file.PathOnClient = 'Service_Agreement_' + Datetime.now().getTime() + '.pdf';
		  file.VersionData = pdfPageBlob;
		  file.FirstPublishLocationId = opp.Id;

		  insert file;

All Answers

SabrentSabrent
The only valid vlaues for the field origin in ContentVersion are C and H

C—Content document from the user's personal library
H—Salesforce file from the user's My Files

Reference: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm



 
Heather_HansonHeather_Hanson
Ahh OK, I thought I had to reference the attachment.

I tried both C and H, but it still only saves to the Attachments and not Files.  I'm not really sure I fully understand the C and H because the document is originating from either of those locations, I don't think.

Is there something else on my code that I'm doing wrong?
Raj VakatiRaj Vakati
Use this code


Refer this link 

https://sagarsindhiblog.wordpress.com/2017/08/29/upload-file-upload-file-as-an-attachment-or-as-a-salesforce-file-related-to-particular-object-using-visualforce-page/

https://www.biswajeetsamal.com/blog/convert-attachment-to-file-in-salesforce-using-apex/


 
public pageReference savePDF(){
    
        Date dateToday = Date.today();
        String sMonth = String.valueof(dateToday.month());
        String sDay = String.valueof(dateToday.day());
        if(sMonth.length()==1){
          sMonth = '0' + sMonth;
        }
        if(sDay.length()==1){
          sDay = '0' + sDay;
        }
        String sToday = String.valueof(dateToday.year())+ '-' + sMonth +  '-' + sDay ;
        
      PageReference pagePdf = new PageReference('/apex/SalesAgreement_PDF');
          pagePdf.getParameters().put('id', idVal);
          Blob pdfPageBlob;
          pdfPageBlob = pagePdf.getContentAsPDF();
                 
          Attachment a = new Attachment();
          a.Body = pdfPageBlob;
          a.ParentID = idVal;
          a.Name = 'Service_Agreement_' + strOppName + '_' + sToday + '.pdf';
          a.Description = 'Service Agreement_' + sToday;
          insert a;
		  
		  
		  ContentWorkspace cw =[SELECT Id FROM ContentWorkspace WHERE DeveloperName = 'Test_Workspace'];
 

          ContentVersion file = new ContentVersion();
		  file.Title = 'Service_Agreement_' + strOppName + '_' + sToday + '.pdf';
		  file.PathOnClient = 'Service_Agreement_' + Datetime.now().getTime() + '.txt';
		  file.VersionData = Blob.valueOf('Service_Agreement_' + 
		  Datetime.now().getTime() + '.txt');
		  file.Origin = 'a';
		  file.FirstPublishLocationId = cw.Id;

		  insert file;         
    
        
        pop_msg='Successfully saved';
        displayPopup = true;  
       
        
        opp.Signature_Date__c = null;
        opp.Signature_Key__c = '';
        opp.Signature_Name__c = '';
        opp.Signature_URL__c = null;
        opp.Signature__c = null;

        
        update opp;
      return new PageReference('/apex/SalesAgreement_HTML?id=' + idVal);
      
        
    }

 
Heather_HansonHeather_Hanson
Hi Raj,

Thanks for the code and reference material.  I've been working on this all morning and still can't figure it out.  I copy and pasted the revised code you provided above and also created the Lookup field in Content Version for Opportunity as mentioned in that first link you provided (the second link didn't work), so my class saved for me, but when I test by trying the "SavePDF" on the visualforce page, I get this error:

List has no rows for assignment to SObject
Error is in expression '{!savePDF}' in page salesagreement_html: Class.SalesAgreement.savePDF: line 56, column 1


This is line 56 in the class:
 
ContentWorkspace cw =[SELECT Id FROM ContentWorkspace WHERE DeveloperName = 'Test_Workspace'];

I also tried a varitey of other things based on the instructions in that first link you provided, but it got pretty confusing and I got to the point where I figured I was likely doing too much (downloaded the jquery toolkit, created the component and referenced it in my Visualforce page), so I went back to the beginning and here I am.

Can you help me figure out what I have overlooked?
Heather_HansonHeather_Hanson
I figured this out and got it working.  I already had an extension created that does SavePDF as an attachment so I just added the following to have it also save as a File:

 
ContentVersion file = new ContentVersion();
		  file.Title = 'Service_Agreement_' + strOppName + '_' + sToday + '.pdf';
		  file.PathOnClient = 'Service_Agreement_' + Datetime.now().getTime() + '.pdf';
		  file.VersionData = pdfPageBlob;
		  file.FirstPublishLocationId = opp.Id;

		  insert file;
This was selected as the best answer