+ Start a Discussion
Markey1Markey1 

Save Record and Attach File on Save - Attachment Field Validation (Sites/Visualforce)

I am trying to attach a file on Save. The code works if the Attachment fields are filled out prior to hitting save. If all of the Attachment fields are not filled out, the record will save but the attachment doesn't.

 

This is a Sites project in which the "guest" user only has create/read access to the "AESK_Test" custom object. If I allow edit rights to the object, everything works.

 

Is there a way to have the record perform the regular validation (i.e. error if form fields and Attachment fields are not filled out), save the record, and then attach the Attachment? If not, is there a way to perform validaiton on just the Attachment fields which would ensure all three fields are filled out prior to saving?

 

Snippet of my Visualforce Page:

          <apex:pageBlockSectionItem >
            <apex:outputLabel value="File Name" for="fileName"/>
            <apex:inputText value="{!attachment.name}" id="fileName" required="true"/>
          </apex:pageBlockSectionItem>
          
          
          <apex:pageBlockSectionItem >
            <apex:outputLabel value="Description" for="description"/>
            <apex:inputTextarea value="{!attachment.description}" id="description" required="true"/>
          </apex:pageBlockSectionItem>          
          
 
          <apex:pageBlockSectionItem >
            <apex:outputLabel value="File" for="file"/>
            <apex:inputFile value="{!attachment.body}" id="file" required="true"/>
          </apex:pageBlockSectionItem>

 

Full Controller:

public class AESK_Test {
 
    //Variables
    public SafeK_Certification__c skTest;
    public String test_us001vereq {get; set;}
    public String test_us001veres {get; set;}
    public String test_us001pareq {get; set;}
    public String test_us001pares {get; set;}    
    public String test_us002vereq {get; set;}
    public String test_us002veres {get; set;}  
    public String test_us002pareq {get; set;}
    public String test_us002pares {get; set;}    
    public String test_us003vereq {get; set;}
    public String test_us003veres {get; set;} 
    public String test_us003pareq {get; set;}
    public String test_us003pares {get; set;}     
    public String test_us005vereq {get; set;}
    public String test_us005veres {get; set;}  
    public String test_us005pareq {get; set;}
    public String test_us005pares {get; set;}   
    
    public String test_tc01crreq {get; set;} 
    public String test_tc01crres {get; set;}  
    public String test_tc02vereq {get; set;} 
    public String test_tc02veres {get; set;} 
    public String test_tc02pareq {get; set;} 
    public String test_tc02pares {get; set;} 
    public String test_tc04vereq {get; set;} 
    public String test_tc04veres {get; set;} 
    public String test_tc04pareq {get; set;} 
    public String test_tc04pares {get; set;}    
    public String test_tc05avereq {get; set;} 
    public String test_tc05averes {get; set;} 
    public String test_tc05apareq {get; set;} 
    public String test_tc05apares {get; set;}   
    public String test_tc08vereq {get; set;} 
    public String test_tc08veres {get; set;} 
    public String test_tc08pareq {get; set;} 
    public String test_tc08pares {get; set;}   
    public String test_tc09vereq {get; set;} 
    public String test_tc09veres {get; set;} 
    public String test_tc09pareq {get; set;} 
    public String test_tc09pares {get; set;} 
    public String test_tc11vereq {get; set;} 
    public String test_tc11veres {get; set;} 
    public String test_tc11pareq {get; set;} 
    public String test_tc11pares {get; set;}   
    public String test_tc13vereq {get; set;} 
    public String test_tc13veres {get; set;} 
    public String test_tc13pareq {get; set;} 
    public String test_tc13pares {get; set;}  
    public String test_tc15vereq {get; set;} 
    public String test_tc15veres {get; set;} 
    public String test_tc15pareq {get; set;} 
    public String test_tc15pares {get; set;}                     
                          
    //Controller
    public AESK_Test(ApexPages.StandardController con){
        skTest = (SafeK_Certification__c)con.getRecord();
    }  
    
    //Attachment
    public Attachment attachment {
    get {
        if (attachment == null)
          attachment = new Attachment();
        return attachment;
    }
    set;
    }              

    //Validate, save, and return page
    public PageReference save() {    

            //attachment
            attachment.ParentId = skTest.Id; //the record the file is attached to
            attachment.OwnerId = UserInfo.getUserId();
            attachment.IsPrivate = false;  

        try {

            String pagert = ApexPages.currentPage().getParameters().get('RecordTypeId');
            /*Issuer*/
            if(pagert == '012Q00000004SoO') {
                skTest.RecordTypeId = '012Q00000004SoO';        
            }
            /*Merchant*/
            else if(pagert == '012Q00000004SoT') {
                skTest.RecordTypeId = '012Q00000004SoT';        
            }     
            /*ACS Vendor*/             
            else if(pagert == '012Q00000004SoY') {
                skTest.RecordTypeId = '012Q00000004SoY';        
            }
            /*MPI Vendor*/        
            else if(pagert == '012Q00000004SoU') {
                skTest.RecordTypeId = '012Q00000004SoU';        
            }             
            /*else {
                return null;
            } */
        
            //set field values
            skTest.US001__c = 'Start: US001VEReq' + '\n' + test_us001vereq + '\n' + 'Stop: US001VEReq' + '\n\n' + 'Start: US001VERes' + '\n' + test_us001veres + '\n' + 'Stop: US001VERes' + '\n\n' +
                                'Start: US001PAReq' + '\n' + test_us001pareq + '\n' + 'Stop: US001PAReq' + '\n\n' + 'Start: US001PARes' + '\n' + test_us001pares + '\n' + 'Stop: US001PARes';
                        
            skTest.US002__c = 'Start: US002VEReq' + '\n' + test_us002vereq + '\n' + 'Stop: US002VEReq' + '\n\n' + 'Start: US002VERes' + '\n' + test_us002veres + '\n' + 'Stop: US002VERes' + '\n\n' +
                                'Start: US002PAReq' + '\n' + test_us002pareq + '\n' + 'Stop: US002PAReq' + '\n\n' + 'Start: US002PARes' + '\n' + test_us002pares + '\n' + 'Stop: US002PARes';
            
            skTest.US003__c = 'Start: US003VEReq' + '\n' + test_us003vereq + '\n' + 'Stop: US003VEReq' + '\n\n' + 'Start: US003VERes' + '\n' + test_us003veres + '\n' + 'Stop: US003VERes' + '\n\n' +
                                'Start: US003PAReq' + '\n' + test_us003pareq + '\n' + 'Stop: US003PAReq' + '\n\n' + 'Start: US003PARes' + '\n' + test_us003pares + '\n' + 'Stop: US003PARes';
            
            skTest.US005__c = 'Start: US005VEReq' + '\n' + test_us005vereq + '\n' + 'Stop: US005VEReq' + '\n\n' + 'Start: US005VERes' + '\n' + test_us005veres + '\n' + 'Stop: US005VERes' + '\n\n' + 
                                'Start: US005PAReq' + '\n' + test_us005pareq + '\n' + 'Stop: US005PAReq' + '\n\n' + 'Start: US005PARes' + '\n' + test_us005pares + '\n' + 'Stop: US005PARes';                        

            skTest.TC01__c = 'Start: TC01CRReq' + '\n' + test_tc01crreq + '\n' + 'Stop: TC01CRReq' + '\n\n' + 'Start: TC01CRRes' + '\n' + test_tc01crres + '\n' + 'Stop: TC01CRRes';

            skTest.TC02__c = 'Start: TC02VEReq' + '\n' + test_tc02vereq + '\n' + 'Stop: TC02VEReq' + '\n\n' + 'Start: TC02VERes' + '\n' + test_tc02veres + '\n' + 'Stop: TC02VERes' + '\n\n' + 
                                'Start: TC02PAReq' + '\n' + test_tc02pareq + '\n' + 'Stop: TC02PAReq' + '\n\n' + 'Start: TC02PARes' + '\n' + test_tc02pares + '\n' + 'Stop: TC02PARes';                        
                                  
            skTest.TC04__c = 'Start: TC04VEReq' + '\n' + test_tc04vereq + '\n' + 'Stop: TC04VEReq' + '\n\n' + 'Start: TC04VERes' + '\n' + test_tc04veres + '\n' + 'Stop: TC04VERes' + '\n\n' + 
                                'Start: TC04PAReq' + '\n' + test_tc04pareq + '\n' + 'Stop: TC04PAReq' + '\n\n' + 'Start: TC04PARes' + '\n' + test_tc04pares + '\n' + 'Stop: TC04PARes';    

            skTest.TC05A__c = 'Start: TC05AVEReq' + '\n' + test_tc05avereq + '\n' + 'Stop: TC05AVEReq' + '\n\n' + 'Start: TC05AVERes' + '\n' + test_tc05averes + '\n' + 'Stop: TC05AVERes' + '\n\n' + 
                                'Start: TC05APAReq' + '\n' + test_tc05apareq + '\n' + 'Stop: TC05APAReq' + '\n\n' + 'Start: TC05APARes' + '\n' + test_tc05apares + '\n' + 'Stop: TC05APARes';                

            skTest.TC08__c = 'Start: TC08VEReq' + '\n' + test_tc08vereq + '\n' + 'Stop: TC08VEReq' + '\n\n' + 'Start: TC08VERes' + '\n' + test_tc08veres + '\n' + 'Stop: TC08VERes' + '\n\n' + 
                                'Start: TC08PAReq' + '\n' + test_tc08pareq + '\n' + 'Stop: TC08PAReq' + '\n\n' + 'Start: TC08PARes' + '\n' + test_tc08pares + '\n' + 'Stop: TC08PARes';    

            skTest.TC09__c = 'Start: TC09VEReq' + '\n' + test_tc09vereq + '\n' + 'Stop: TC09VEReq' + '\n\n' + 'Start: TC09VERes' + '\n' + test_tc09veres + '\n' + 'Stop: TC09VERes' + '\n\n' + 
                                'Start: TC09PAReq' + '\n' + test_tc09pareq + '\n' + 'Stop: TC09PAReq' + '\n\n' + 'Start: TC09PARes' + '\n' + test_tc09pares + '\n' + 'Stop: TC09PARes';    
                                
            skTest.TC11__c = 'Start: TC11VEReq' + '\n' + test_tc11vereq + '\n' + 'Stop: TC11VEReq' + '\n\n' + 'Start: TC11VERes' + '\n' + test_tc11veres + '\n' + 'Stop: TC11VERes' + '\n\n' + 
                                'Start: TC11PAReq' + '\n' + test_tc11pareq + '\n' + 'Stop: TC11PAReq' + '\n\n' + 'Start: TC11PARes' + '\n' + test_tc11pares + '\n' + 'Stop: TC11PARes';                                                

            skTest.TC13__c = 'Start: TC13VEReq' + '\n' + test_tc13vereq + '\n' + 'Stop: TC13VEReq' + '\n\n' + 'Start: TC13VERes' + '\n' + test_tc13veres + '\n' + 'Stop: TC13VERes' + '\n\n' + 
                                'Start: TC13PAReq' + '\n' + test_tc13pareq + '\n' + 'Stop: TC13PAReq' + '\n\n' + 'Start: TC13PARes' + '\n' + test_tc13pares + '\n' + 'Stop: TC13PARes';     

            skTest.TC15__c = 'Start: TC15VEReq' + '\n' + test_tc15vereq + '\n' + 'Stop: TC15VEReq' + '\n\n' + 'Start: TC15VERes' + '\n' + test_tc15veres + '\n' + 'Stop: TC15VERes' + '\n\n' + 
                                'Start: TC15PAReq' + '\n' + test_tc15pareq + '\n' + 'Stop: TC15PAReq' + '\n\n' + 'Start: TC15PARes' + '\n' + test_tc15pares + '\n' + 'Stop: TC15PARes'; 
                                                                                                       
            insert skTest;                                                            
            insert attachment; 
        } 
        catch (DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        finally {
            attachment = new Attachment();
        }
    return page.aesktestconfirm; 
    } 
}