+ Start a Discussion
Michael Webb 15Michael Webb 15 

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

Hello,

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.

Controller 

public class SubmitCaseController {     
    public Case c { get; set; }
    // public String acctNum { get; set; }
    public SelectOption[] caseTypes { get; set;}
    
    public List<Case> UserCaseList
    {
        get
        {
            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
    {
        get
        {
            
            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');
            //ApexPages.addMessage(msg);
            //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 = 'Salesforce.com';
                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) {
                ApexPages.addMessages(e);
                return null;
            }
        }
    }

VF Page 

<apex:page standardController="Case" extensions="SubmitCaseController" tabStyle="CTSB_SF_Support__tab">
    <style>
        .tableheader{
        text-align:Center; 
        padding-bottom: 10px; 
        }
        .tablecell{
        text-align:left; 
        padding-bottom: 10px; 
        }
        .tablecustom
        {
        font-size:larger;
        }
    </style>
    <apex:relatedList list="CaseList" />
    <apex:form >
        
        <apex:pageBlock title="CTSB Salesforce.com Case">   
            <apex:pageMessages />
            <apex:pageBlockSection title="Contact Info">
                
                <table class="tablecustom">
                    <tr>
                        <th class="tableheader">Name:</th> 
                        <td class="tablecell">{!$User.FirstName} {!$User.LastName}</td>
                    </tr>
                    
                    <tr>
                        <th style="text-align:right">Email:</th>
                        <td class="tablecell">{!$User.Email}</td>
                    </tr>
                </table>
            </apex:pageBlockSection>
            <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:selectList>
                <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:pageBlockSection>
            <apex:pageBlockTable value="{!UserCaseList}" var="c">
                <apex:column >
                    <apex:facet name="header">
                        <apex:outputLabel value="{!$ObjectType.Case.Fields.CaseNumber.Label}" />
                    </apex:facet>
                    <apex:outputLink value="/{!c.id}" target="_blank">{!c.CaseNumber}</apex:outputLink>
                </apex:column>
                <apex:column value="{!c.Subject}" />
                <apex:column value="{!c.Status}" />
                <apex:column value="{!c.Origin}" />
                <apex:column value="{!c.ClosedDate}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
        
    </apex:form>
    
</apex:page>

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!

 
pconpcon
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.

[1] http://blog.jeffdouglas.com/2010/04/28/uploading-an-attachment-using-visualforce-and-a-custom-controller/