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
Michael Webb 15Michael Webb 15 

Need to create process for adding attachment to VF page for Case Submit Page.


I tried a few different times to create a attach file when creating a case with this page. I keep getting errors (I am new to Apex so thank you in advance).

Here is what I have the VF page and the controller.


public class SubmitCaseController {     
    public Case c { get; set; }
    // public String acctNum { get; set; }
    public SelectOption[] caseTypes { get; set;}
    public List<Case> UserCaseList
            return [Select Status, Origin, ClosedDate, CaseNumber, Subject
                    From Case
                    Where Contact.Email = :UserInfo.getUserEmail() AND Origin = 'SF' limit 10];
    public SubmitCaseController(ApexPages.StandardController controller) {
        c = new Case();
        this.caseTypes = new SelectOption[]{};
        this.caseTypes.add(new SelectOption('-', '-'));
        this.caseTypes.add(new SelectOption('Question','Question'));
        this.caseTypes.add(new SelectOption('Problem', 'Problem'));
        this.caseTypes.add(new SelectOption('Enhancement', 'Enhancement'));
        this.caseTypes.add(new SelectOption('Training', 'Training'));
    public Contact userContact
            Contact cnt = [SELECT Id FROM Contact WHERE Email = :c.SuppliedEmail LIMIT 1];
            if (cnt != null) return cnt;
            else return null;

    public PageReference submitCase() {
       // List<Account> accts = [SELECT Id FROM Account WHERE AccountNumber = :acctNum Limit 100];

        // if (accts.size() != 1) {
            //ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Invalid account number');
            //return null;
      //  } else {
            try {
                if (c.Type == '-') 
                     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Invalid Type Selected'));
                     return null;

                // c.AccountId = accts.get(0).Id;  
                c.SuppliedEmail = UserInfo.getUserEmail();
                c.Origin = 'SF';
                //c.RecordTypeId = '012W00000000Xmb';
                c.RecordTypeId = '012000000004yTO';
                c.Product__c = '';
                c.OwnerId = '00500000007HkiF';
                if (c.Customer_Facing__c == TRUE) {
                c.Priority = 'High -2';
                } else {(c.Customer_Facing__c = FALSE);
                (c.Priority = 'Med - 3');
                // now look for an associated contact with the same email
                Contact cnt = userContact;
                if (cnt != null) c.ContactId = cnt.Id;
                // Specify DML options to ensure the assignment rules are executed
                // Database.DMLOptions dmlOpts = new Database.DMLOptions();
                // dmlOpts.assignmentRuleHeader.useDefaultRule = true;
                // c.setOptions(dmlOpts); 
                // Insert the case
                INSERT c;
                return new PageReference('/home/home.jsp');
            } catch (Exception e) {
                return null;

VF Page 

<apex:page standardController="Case" extensions="SubmitCaseController" tabStyle="CTSB_SF_Support__tab">
        padding-bottom: 10px; 
        padding-bottom: 10px; 
    <apex:relatedList list="CaseList" />
    <apex:form >
        <apex:pageBlock title="CTSB Case">   
            <apex:pageMessages />
            <apex:pageBlockSection title="Contact Info">
                <table class="tablecustom">
                        <th class="tableheader">Name:</th> 
                        <td class="tablecell">{!$User.FirstName} {!$User.LastName}</td>
                        <th style="text-align:right">Email:</th>
                        <td class="tablecell">{!$User.Email}</td>
            <apex:pageBlockSection id="RedSection" columns="1" title="Issue Description">
                <apex:inputText required="true" value="{!c.Subject}"/>
                <apex:selectList size="1" value="{! c.Type}">
                    <apex:selectOptions value="{! caseTypes}" />
                <apex:inputField value="{!c.Area__c}"/>
                <apex:inputField value="{!c.Related_Record__c}"/>
                <apex:inputTextArea required="true" rows="3" value="{!c.Description}" />
                 <apex:inputField value="{!c.Customer_Facing__c}"/>
                <apex:commandButton value="Submit Case" action="{!submitCase}"/>
            <apex:pageBlockTable value="{!UserCaseList}" var="c">
                <apex:column >
                    <apex:facet name="header">
                        <apex:outputLabel value="{!$ObjectType.Case.Fields.CaseNumber.Label}" />
                    <apex:outputLink value="/{!}" target="_blank">{!c.CaseNumber}</apex:outputLink>
                <apex:column value="{!c.Subject}" />
                <apex:column value="{!c.Status}" />
                <apex:column value="{!c.Origin}" />
                <apex:column value="{!c.ClosedDate}" />

Any help would be extremely helpful, I was able to do quite a bit for my first VF and Apex controller, I assumed this would be easy but I just can't get it.

Thank you in advance!

I don't see where you are even handling attachments anywhere in your controller or you visualforce page.  Take a look over this [1] and if you have any additional questions, please let me know.

NOTE: When adding code, please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.