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
obrienobrien 

Help: Adding an attachment to a Case record using an extension & VF page

Hello,

 

I'm creating a page that is bound to a Case Standard Controller and I've created an extension to modify the "New Case" creation process. I want the users to be able to upload an attachment while filling out the New Case details but I've come across an issue. Here is the extension:

public class CaseExt{
    
    public ApexPages.StandardController controller {get;set;}
    public Case c {get;set;}
    public String userEmail;
    Public Attachment a {get;set;}

    public CaseExt(ApexPages.StandardController con){
        controller = con;
        c = (Case) controller.getRecord();
        a = new Attachment();
        userEmail = userInfo.getUserName();
    }
    
    public PageReference save(){
     List<Contact> cnt = [SELECT Id, Email, Name FROM Contact WHERE Email =: userEmail];
        if (cnt.size() < 1) {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.WARNING, 'Your email address does not match our records. Please update your contact information.');
            ApexPages.addMessage(msg);
            return null;
        } else {
            try {
                c.ContactId = cnt.get(0).Id;
                INSERT c;  
                a.ParentId = c.Id;
                INSERT a;
                return page.CaseThankYou;           
            }catch (Exception e){
            ApexPages.addMessages(e);
            return null;
            }
        }
     }
}

 And the VF page:

<apex:page StandardController="case" extensions="CaseExt">
    <apex:form >
      <apex:pageBlock title="Submit A Case">
        <apex:pageBlockButtons location="bottom">
        <apex:commandButton action="{!save}" value="Submit"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection columns="1">                     
        <apex:inputfield value="{!c.type}" required="true"/>
        <apex:inputField value="{!c.priority}"/>
        <apex:inputField value="{!c.Subject}" required="true"/>   
        <apex:inputTextarea value="{!c.description}" rows="8" cols="50" />
        <apex:inputfile value="{!a.body}" filename="{!a.name}" />
        </apex:pageBlockSection>
     </apex:pageBlock>
   </apex:form>
</apex:page>

 

The issue I'm seeing is that when I save the record the attachment doesn't appear to be added to the case.. I can't seem to figure out why. Any help or recommendations are greatly appreciated.

 

Thank you 

 

 

J

Laxman RaoLaxman Rao

Might be no contact records were retrieved because you are quering by currentLoginedUser.

You are using userEmail = userInfo.getUserName();

 

I have executed your code, it was working for me.

once try this I have added page messages:

<apex:page StandardController="case" extensions="CaseExt">
    <apex:form >
      <apex:pageMessages ></apex:pageMessages>
      <apex:pageBlock title="Submit A Case">
        <apex:pageBlockButtons location="bottom">
        <apex:commandButton action="{!save}" value="Submit"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection columns="1">                     
        <apex:inputfield value="{!c.type}" required="true"/>
        <apex:inputField value="{!c.priority}"/>
        <apex:inputField value="{!c.Subject}" required="true"/>   
        <apex:inputTextarea value="{!c.description}" rows="8" cols="50" />
        <apex:inputfile value="{!a.body}" filename="{!a.name}" />
        </apex:pageBlockSection>
     </apex:pageBlock>
   </apex:form>
</apex:page>

 

Let me know if you any issues

obrienobrien

All of our usernames are the user's email address - the part of this extension that assigns the currently logged in user as the contact for the case is working properly, however, after the case is submitted I go into the case details to verify that the attachment was uploaded and the attachment isn't there. Is it possible that the attachment is uploading but for some reason it is not viewable in the case details?

 

I have added <apex:pageMessages > but I wasn't prompted with any messages when submitting the case.

 

Thanks for your assitance with this!

 

 

edit: wording

obrienobrien

Resolved... beginner's mistake: the attachments section in the page layout was hidden :|