1

I have a requirement where user should have option to add attachment before saving the case record. Currently i am creating a case using visualforce page. please suggest how can i achieve this?

Vf page to create case record:

<apex:page standardController="Case" extensions="AutoPopulateExample">

    <apex:form id="form">
        <apex:pageBlock >

            <apex:pageBlockButtons >
              <apex:commandButton value="Save" action="{!Save}"/>
              <apex:commandButton value="Cancel" action="{!Cancel}"/>
            </apex:pageBlockButtons> 

           <apex:pageBlockSection title="Basic Details" columns="2">
               <apex:inputField label="Contact Name" value="{!Case.ContactId}" >
                <apex:actionSupport event="onchange" action="{!autoCal}" reRender="form"/>
               </apex:inputField>
               <apex:inputField label="Requestor First Name" value="{!Case.Requestor_First_Name__c}"/>
               <apex:inputField label="Account Name" value="{!Case.AccountId}"/>
               <apex:inputField label="Requestor Last Name" value="{!Case.Requestor_Last_Name__c}"/>
               <apex:inputField label="Status" value="{!Case.Status}"/>
               <apex:inputField label="Requestor Email" value="{!Case.Requester_Email__c}"/>
               <apex:inputField label="Subject" value="{!Case.Subject}"/>
               <apex:inputField label="Requestor Contact Number" value="{!Case.Requester_Contact_Number__c}"/>
               <apex:inputField label="Product" value="{!Case.Products__c}"/>
               <apex:inputField label="Business Impact" value="{!Case.Business_Impact__c}"/>
               <apex:inputField label="Components" value="{!Case.Componet__c}"/>
               <apex:inputField label="Business Urgency" value="{!Case.Business_Urgency__c}"/>
               <apex:inputField label="On Behalf of" value="{!Case.On_Behalf_of__c}"/>
               <apex:inputField label="Environment" value="{!Case.Environment__c}"/>
               <apex:inputField label="Description" value="{!Case.Description__c}"/>
           </apex:pageBlockSection>

<apex:pageBlockSection title="Attachment Details" columns="2" id="block1">


    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File Name" for="fileName"/>
      <apex:inputText value="{!attachmentObject.name}" id="fileName"/>
    </apex:pageBlockSectionItem>

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File" for="file"/>
      <apex:inputFile value="{!attachmentObject.body}" filename="{!attachmentObject.name}" id="file"/>
    </apex:pageBlockSectionItem>

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="Description" for="description"/>
      <apex:inputTextarea value="{!attachmentObject.description}" id="description"/>
    </apex:pageBlockSectionItem>

  </apex:pageBlockSection>    
    </apex:pageBlock>
     </apex:form>
</apex:page>

Controller :

public class AutoPopulateExample
{
public Contact con{get;set;}
public Case caseObject{get;set;}
public Attachment attachmentObject {get; set;}
public ApexPages.StandardController stdCntrlr {get; set;}

public AutoPopulateExample(ApexPages.StandardController controller)
{
    con = new Contact();
    caseObject = new case();
    caseObject = (Case)controller.getRecord();

    this.stdCntrlr=controller;
     this.attachmentObject = new Attachment();
}

//function is called from actionsupport event

public void autoCal()
{

Id conid = caseObject.ContactId;     // collecting contact id from visualforce page

List<Contact> conLst = [select id,AccountId,FirstName,LastName,Email,Phone from contact where id=:conid];

    if(conLst.isEmpty())
    {
      return;
    }


caseObject.Requestor_First_Name__c = conLst[0].FirstName;      
caseObject.AccountId = conLst[0].AccountId; 
caseObject.Requestor_Last_Name__c = conLst[0].LastName;
caseObject.Requester_Email__c = conLst[0].Email; 
caseObject.Requester_Contact_Number__c = conLst[0].Phone; 


}

    public PageReference save() {

        // Save the Case
        PageReference pr = stdCntrlr.save();

        // Save the Attachment using the Case id as the parent Id
        attachmentObject.ParentId = stdCntrlr.getId();
        insert attachmentObject;

        // Go wherever the standard controller wnts
        return pr;
    }
}

1 Answer 1

2

You can use an apex:inputFile for this as illustrated in Uploading an attachment using inputfile tag.

So in the save method, first save the Case, then set the Id for that Case as the ParentId of the Attachment and save the Attachment.

Assuming you are using the StandardController save method, the changes would be something like this in the page:

<apex:inputfile value="{!attachmentObject.Body}" filename="{!attachmentObject.Name}"/>

with controller changes of:

public with sharing class AutoPopulateExample {

    ...
    public Case caseObject {get;set;}
    public Attachment attachmentObject {get; set;}

    private ApexPages.StandardController sc;

    public AutoPopulateExample(ApexPages.StandardController sc) {

        this.sc = sc;
        ...
        this.attachmentObject = new Attachment();
        ...
    }

    public PageReference save() {

        // Save the Case
        PageReference pr = sc.save();

        // Save the Attachment using the Case id as the parent Id
        attachmentObject.ParentId = sc.getId();
        insert attachmentObject;

        // Go wherever the standard controller wnts
        return pr;
    }
}
3
  • Hi Keith thank you for response. I have made the changes in code as suggested. but it is showing me below error on vf page. "Unknown property 'CaseStandardController.attachment'" I am not able to add code to show you.
    – Snehal
    Jan 22, 2019 at 11:14
  • i have corrected that error and getting vf page as expected. but when i try to select contact name (lookup on case) it is giving me error "apex:inputFile can not be used in conjunction with an action component, apex:commandButton or apex:commandLink that specifies a rerender or oncomplete attribute. "
    – Snehal
    Jan 22, 2019 at 12:01
  • @SnehalGaware I don't know what your problem is. I suggest you persevere for a couple of hours trying to figure it out (Google is your friend) - you have a fairly explicit explanation of the problem in the error message you get. If you remain stuck post a new question with your full page and full controller and an exact copy of the error message.
    – Keith C
    Jan 22, 2019 at 13:33

You must log in to answer this question.

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