Tuesday, 22 December 2015

How to create a case record from a VF page and attach a particular file to the record?

Below is code snippet for the same.


VF Page:-
<apex:page controller="creatingcaseAttachment">
  <apex:form >
      <apex:pageBlock title="Customer Issue">
      
          <apex:pageBlockSection title="Case Creation ">
              <apex:inputField value="{!customerCase.accountid}"/>  
              <apex:inputField value="{!customerCase.contactId}"/>  
              <apex:inputField value="{!customerCase.Origin}"/>  
              <apex:inputField value="{!customerCase.Status}"/>  
              <apex:inputField value="{!customerCase.Reason}"/>
              <apex:inputField value="{!customerCase.Subject}"/>  
              
              <apex:outputPanel style="float:right;">
                <apex:inputfile value="{!objAttachment.body}" filename="{!objAttachment.name}" ></apex:inputfile>
                <apex:commandButton value="Create Case" action="{!CaseCreation}"/>
               </apex:outputPanel>
               
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page> 

Controller:-
public with sharing class creatingcaseAttachment {
    
    public Case customerCase{get;set;}
     Public attachment objAttachment{get; set;}
    
    public creatingcaseAttachment (){
        customerCase = new Case();
        objAttachment = new Attachment();
    } 
     Public PageReference CaseCreation(){
        Insert customerCase;
        
        objAttachment.ParentId = customerCase.id;
        insert objAttachment;
        
        PageReference p  = new PageReference('/'+customerCase.id);
        p.setRedirect(true);
        return p;
    }
}

No comments: