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
jsacpt24jsacpt24 

VisualForce Attachment isn't uploading

I am trying to have a site that has a visualforce page allowing someone to upload an attachment with the new record that they are creating. I have the attachment passing at 95% code coverage but it doesn't seem to actually be uploading the attachment with the new record.

VF: 
<apex:pageBlockButtons >
    <apex:commandButton action="{!save}" value="Submit"/>
       <apex:commandButton action="{!cancel}" value="Cancel"/>
    </apex:pageBlockButtons>
    <apex:pageBlockSection title="Upload the Attachment" collapsible="false" dir="LTR" columns="1">
        <div id="upload" class="upload">                                   
            <apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/>                            
        </div>
</apex:pageBlockSection>

Apex Class: 
public class dataSheetAttachment
{
    public Data_Feasibility_Request__c objdfr{get;set;}
    public Attachment myAttachment{get;set;}
    public string fileName{get;set;}
    public Blob fileBody{get;set;}

    public dataSheetAttachment(Apexpages.standardcontroller controller)
    {
        objdfr = (Data_Feasibility_Request__c)controller.getRecord();
        myAttachment = new Attachment();
    }
    public pagereference save()
    {
        if(myAttachment.Name == null)
        {
            insert objdfr;
        }
        if(myAttachment.Name != null)
        {
            insert objdfr;
            System.debug('@@@@@fileBody'+fileBody);    
            myAttachment = new Attachment();
            Integer i=0;
            myAttachment .clear();
            myAttachment.Body = fileBody;
            myAttachment.Name = fileName ;
            myAttachment.ParentId = objdfr.id;            
            insert myAttachment;   
        }             
       pagereference pr = Page.Thank_You;                          
       pr.setRedirect(true);
       return pr;
    }
}
Test Class: 
 
@isTest
private class Test_dataSheetAttachment {

public static testMethod void validatedataSheetAttachment() {        
    Data_Feasibility_Request__c myDFR = new Data_Feasibility_Request__c();
    dataSheetAttachment pageController = new dataSheetAttachment(new ApexPages.StandardController(myDFR));
  
    pageController.myAttachment.Name = 'foo';
    pageController.fileBody=Blob.valueOf('Body Test');
    pageController.fileName = 'Test';
    pageController.save();
  
    }

}



Wondering if someone might be able to help me figure out what I might be doing incorrect? 
Best Answer chosen by jsacpt24
Amit Chaudhary 8Amit Chaudhary 8
Try to update your cide like below
public class dataSheetAttachment
{
    public Data_Feasibility_Request__c objdfr{get;set;}
    public Attachment myAttachment{get;set;}
    public string fileName{get;set;}
    public Blob fileBody{get;set;}

    public dataSheetAttachment(Apexpages.standardcontroller controller)
    {
        objdfr = (Data_Feasibility_Request__c)controller.getRecord();
        myAttachment = new Attachment();
    }
    public pagereference save()
    {
		if( fileName != null )
		{
			insert objdfr;
			System.debug('@@@@@fileBody'+fileBody);    
			
			myAttachment = new Attachment();
			Integer i=0;
			myAttachment .clear();
			myAttachment.Body = fileBody;
			myAttachment.Name = fileName ;
			myAttachment.ParentId = objdfr.id;            
			insert myAttachment;   
		}
		
		pagereference pr = Page.Thank_You;                          
		pr.setRedirect(true);
		return pr;
    }
}

Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Try to update your cide like below
public class dataSheetAttachment
{
    public Data_Feasibility_Request__c objdfr{get;set;}
    public Attachment myAttachment{get;set;}
    public string fileName{get;set;}
    public Blob fileBody{get;set;}

    public dataSheetAttachment(Apexpages.standardcontroller controller)
    {
        objdfr = (Data_Feasibility_Request__c)controller.getRecord();
        myAttachment = new Attachment();
    }
    public pagereference save()
    {
		if( fileName != null )
		{
			insert objdfr;
			System.debug('@@@@@fileBody'+fileBody);    
			
			myAttachment = new Attachment();
			Integer i=0;
			myAttachment .clear();
			myAttachment.Body = fileBody;
			myAttachment.Name = fileName ;
			myAttachment.ParentId = objdfr.id;            
			insert myAttachment;   
		}
		
		pagereference pr = Page.Thank_You;                          
		pr.setRedirect(true);
		return pr;
    }
}

Let us know if this will help you
 
This was selected as the best answer
jsacpt24jsacpt24
Looks like that only allowed for a submission if there was an attachment but if not it still acted like it submitted but it didn't. BUT! it made something click that allowed me to realize the mistake that I made. I needed to switch the order and then put in an else instead of a second if. 
jsacpt24jsacpt24
If I wanted to them make this into allowing them to have this go into a multi-attachment capabilities is there a way you recommend doing that?