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

adding attachment on case object while creating a case

Help: Add attachment to Case record using Visualforce "apex:inputFile" component?


I am building a Visualforce page that is bound to the Case standard controller. This page is replacing the standard New Case page in I want to include an input that allows users to add an attachment to their Case when they are creating the record using my page. I already have a controller extension that this page is using to enhance the New Case process.
Hello Dasari,
As i understood about your requirment
i am sending vf page and class i feel this suits your requirement.

VF Page:
<apex:page standardController="case" extensions="caseattachment">
    <apex:form id="frm">
        <apex:pageBlock title="Case Edit">
            <apex:pageBlockSection title="Case Information" collapsible="false">
                    <apex:outputLabel value="Case Owner"/>     
                    <apex:outputlabel value="{!$User.FirstName} {!$User.LastName}"/>
                    <apex:inputField value="{!objcase.Status}"/>
                    <apex:inputField value="{!objcase.Type}"/>
                    <apex:inputField value="{!objcase.Origin}"/>               
            <apex:commandButton value="Save" action="{!save}"/>
        <apex:pageBlockSection title="Uploading the Attachment" collapsible="false" dir="LTR" columns="1">
        <div id="upload" class="upload">                                   
            <apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/>                            

Apex Class:

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

    public caseattachment(Apexpages.standardcontroller controller)
        objcase = new case();
        myAttachment =new Attachment();
    public pagereference save()
        insert objcase;
        myAttachment  = new Attachment();
              Integer i=0;
              myAttachment .clear();
              myAttachment.Body = fileBody; 
              myAttachment.Name = 'Logo_''.jpeg' ; 
              myAttachment.ParentId =;             
              insert myAttachment;                 
        pagereference pr = new pagereference('/';                           
        return pr;

if you get any doubts in this please feel free to ask me

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
SFDC Certified Developer

Rimsha ShabbirRimsha Shabbir
Hello Sri,

Would you be able to help me with a similar issue. I have an aura component for creating a new case, as needed custom layout. Could you help adding the attachments option to it?