1

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 working.

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;
    }
}

Wondering if anyone knows what I might be missing?

3
  • 1
    What do you mean when you say it's not working?
    – Raul
    Commented Mar 28, 2017 at 19:49
  • The save is successful and a new record is created which is expected but when I try to use the upload function on the for the attachment the record is still created without an attachment on the record.
    – SoleHack
    Commented Mar 28, 2017 at 19:57
  • @JoshuaAnderson, please check my answer Commented Mar 29, 2017 at 4:31

1 Answer 1

2

In your case the issue is myAttachment.Name is always null. That's why it is not attaching the file. Either you have assign this value in page or controller otherwise remove this if condition.

I have written this code and tested that file is getting attached to Account record. Though I am not creating Account record. Just taking the reference of AccountId and attaching to that record.

Visualforce

No changes have been made other than declaring <apex:page>

<apex:page StandardController="Account" extensions="AccountAttachment">
    <apex:form >
        <apex:pageBlock >
            <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:pageBlock>
    </apex:form>
</apex:page>

Controller

public class AccountAttachment
{
    public Account objdfr{get;set;}
    public Attachment myAttachment{get;set;}
    public String fileName{get;set;}
    public Blob fileBody{get;set;}
    public Id accountId {get;set;}

    public AccountAttachment(Apexpages.standardcontroller controller)
    {
        objdfr = (Account)controller.getRecord();
        accountId = (Id) controller.getId();
        myAttachment = new Attachment();
    }
    public pagereference save()
    {      
        System.debug('@@@@@fileBody'+fileBody);    
        myAttachment = new Attachment();
        myAttachment.Body = fileBody;
        myAttachment.Name = fileName;
        myAttachment.ParentId = accountId;            
        insert myAttachment;   

        return (new ApexPages.StandardController (new Account(Id=accountId))).view();
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .