I am trying to have an external site for our clients to be able to submit cases to our team. I have the VF set in place to have them submit a case and allow them to be able to add an attachment for a screenshot or more documentation. When they submit the record with an attachment the record is created (as expected) but it doesn't look like the attachment comes over at all. I feel like it is not reading the myAttachment.Name but the file that I insert has a name hence the it being able to be saved.

Am I taking the wrong approach to this?


        <apex:page Standardcontroller="Case" extensions="attachController" showheader="false" >
<html lang="en">
        <link rel="icon" type="image/png" href="{!$Resource.siteLogo}" />
        <link href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" rel="stylesheet" media="screen"/>

        <!-- Bootstrap -->
        <link href="https://cdn.rawgit.com/creativetimofficial/now-ui-kit/2e8e665f/assets/css/bootstrap.min.css" rel="stylesheet" media="screen"/>
        <link href="https://cdn.rawgit.com/creativetimofficial/now-ui-kit/2e8e665f/assets/css/now-ui-kit.min.css?v1.2.0" media="screen"/>
        <title>Submit a Case</title>

            p, li, .description{
            font-weight: 400;
            line-height: 1.8rem;

            .paddingTop{padding-top: 2rem;}

            .background-black{background: #2196f3;}

            .background-black:focus{background: #2386d3 !important;}      

            section .section-inner h2{color: #fff;}

            .round {
            border-radius: 100px;
            margin: 0;
            color: #9a9a9a

            .longBox{height: 100px;}

            @media screen and (max-width: 500px){
            .brand .h1-seo{
            font-size: 2.2rem;


        <div class="container">
            <apex:form styleclass="form-signin"><br/><br/><br/>
                <br/><h2 class="form-signin-heading">Create a Ticket</h2><br/><br/>
                <h5 class="form-signin-heading">Email</h5><br/>
                <apex:inputfield styleClass="inputGroupSelect01 custom-select form-control" value="{!case.Email__c}" required="true"/>
                <br/><h5 class="form-signin-heading">Subject</h5><br/>
                <apex:inputfield styleClass="inputGroupSelect01 custom-select form-control" value="{!case.Subject}" required="true"/>
                <br/><h5 class="form-signin-heading">What are you experiencing?</h5><br/>
                <apex:inputfield styleClass="inputGroupSelect01 custom-select form-control" value="{!case.What_are_you_experiencing__c}" required="true"/>
                <br/><h5 class="form-signin-heading">Where is the location of your issue?</h5><br/>
                <apex:inputfield styleClass="inputGroupSelect01 custom-select form-control" value="{!case.Location_of_issue__c}" required="true"/>
                <br/><h5 class="form-signin-heading">Source Type</h5><br/>
                <apex:inputfield styleClass="inputGroupSelect01 custom-select form-control" value="{!case.Source_Type__c}" required="true"/>
                <br/><h5 class="form-signin-heading">Description</h5><br/>
                <apex:inputfield styleClass="inputGroupSelect01 custom-select form-control longBox" value="{!case.Description}" required="true"/><br/>
                <apex:commandButton action="{!submit}" value="Submit" styleClass="btn btn-primary btn-block btn-lg background-black btn-round round"/><br/>
                <!-- <button class="btn btn-primary btn-block btn-lg background-black btn-round round " action="{!submit}">Submit</button><br/><br/><br/> -->
                <apex:pageBlock >
                    <apex:pageBlockSection columns="1" >
                        <div id="upload" class="upload">  
                                If you have more than 1 file to upload please zip file to include all files. 
                            <apex:inputFile title="Choose File" value="{!filebody}" filename="{!filename}" styleClass="btn btn-primary btn-block btn-lg background-black btn-round round"/>                            
        <!-- /container -->
        <!-- Bootstrap core JavaScript
================================================== -->
        <!-- Placed at the end of the document so the pages load faster -->


    public class attachController
public case objcase{get;set;}
public Attachment myAttachment{get;set;}
public string fileName{get;set;}
public Blob fileBody{get;set;}

public attachController(Apexpages.standardcontroller controller)
    objcase = (Case)controller.getRecord();
    myAttachment = new Attachment();
public PageReference submit(){
    if (fileName != null){
        objcase.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Product Case').getRecordTypeId();
        insert objcase;
    } else {
        objcase.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Product Case').getRecordTypeId();
        insert objcase;
        myAttachment = new Attachment();
        Integer i=0;
        myAttachment .clear();
        myAttachment.Body = fileBody;
        myAttachment.Name = fileName ;
        myAttachment.ParentId = objcase.id;            
        insert myAttachment;   
    pagereference pr = Page.thankYouForSubmittingYourProductCase;                          
    return pr;

1 Answer 1


Do not put a form inside a form. It will not work the way you expect. Instead, put a div around your apex:form and apply any classes you like there. It should have the same effect.

Update: Also, you're checking myAttachment.Name for null, but it will always be null, because you have not yet assigned the value from fileName to it. Check fileName first:

if(fileName == null) {
  // ...
} else {
  // ...
  • I updated the vf on my original post. I got rid of the extra form and it saved but when I tried to test it the record was never created.
    – SoleHack
    Commented Mar 2, 2019 at 1:59
  • @JoshuaAnderson you are checking the wrong variable. See updated answer.
    – sfdcfox
    Commented Mar 2, 2019 at 3:12
  • Looks like everything is now working. I updated the submission so that it can be referenced and I appreciate it. If I wanted to move this to files instead of to the Notes & Attachments. Is that something that I would do like public File myAttachment{get;set;} or would I have to do that a different way?
    – SoleHack
    Commented Mar 2, 2019 at 3:35
  • @JoshuaAnderson It's more complicated than that. You'll want to look at ContentDocument, which has several related items as well.
    – sfdcfox
    Commented Mar 2, 2019 at 4:09

You must log in to answer this question.

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