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
dasari123dasari123 

adding attachment on case object while creating a case

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

Hello,



I am building a Visualforce page that is bound to the Case standard controller. This page is replacing the standard New Case page in Salesforce.com. 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.
Sri549Sri549
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:pageBlockSectionItem>
                    <apex:outputLabel value="Case Owner"/>     
                    <apex:outputlabel value="{!$User.FirstName} {!$User.LastName}"/>
                </apex:pageBlockSectionItem>
                    <apex:inputField value="{!objcase.Status}"/>
                    <apex:inputField value="{!objcase.Type}"/>
                    <apex:inputField value="{!objcase.Origin}"/>               
            </apex:pageBlockSection>      
        <apex:pageBlockButtons>
            <apex:commandButton value="Save" action="{!save}"/>
        </apex:pageBlockButtons>       
        <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"/>                            
        </div> 
         </apex:pageBlockSection>           
       </apex:pageBlock>        
    </apex:form>
</apex:page>

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;
        System.debug('@@@@@fileBody'+fileBody);     
        myAttachment  = new Attachment();
              Integer i=0;
              myAttachment .clear();
              myAttachment.Body = fileBody; 
              myAttachment.Name = 'Logo_'+objcase.id+'.jpeg' ; 
              myAttachment.ParentId = objcase.id;             
              insert myAttachment;                 
        pagereference pr = new pagereference('/'+objcase.id);                           
        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,
Srinivas
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?

Thanks