0

I'm new to SF and trying to write a custom case controller that adds an attachment to a case.

I've simplified my apex code below. I'm guessing I need to do a.OwnerId = UserInfo.getUserId(); to get the attachment to show up under the user's case list?

I would like to delete an attachment I used for a test but I can't find it.

public class MyCaseController {

    public Case c { get; set; }
    public Attachment a { get; set; }

    public MyCaseController() {
        c = new Case();
        a = new Attachment();        
    }

public PageReference save() {
        try {
            INSERT c;
            if (a.BodyLength > 0)
            {
                a.ParentId = c.Id;
                INSERT a;
            }
            return new PageReference('/apex/Thanks');
        } catch (Exception e) {
            ApexPages.addMessages(e);
            return null;
        }
}


<apex:page controller="MyCaseController" sidebar="False" showHeader="False">
    <apex:form >
        <apex:pageMessages ></apex:pageMessages><br />
        <apex:commandButton action="{!save}" value="Submit"/><br /><br />
        Issue type:<br />
        <apex:inputfield value="{!c.type}" required="true"/><br />
        Issue priority:<br />
        <apex:inputField value="{!c.priority}"/><br />
        Subject:<br />
        <apex:inputField value="{!c.Subject}" required="true"/><br />
        <apex:inputTextarea value="{!c.description}" rows="20" cols="20" /><br />
        Attachment:<br />
        <apex:inputFile value="{!a.body}" filename="{!a.name}" id="file"/>
    </apex:form>
</apex:page>
0
/Simple Custom Controller to Insert an Attachment into Salesforce
public with sharing class AttachmentUploadController 
{
public String parentId {get;set;}
public Attachment attach {get;set;}

public AttachmentUploadController()
{
    attach = new Attachment();
}

//When user clicks upload button on Visualforce Page, perform upload/insert
//Redirect user to newly inserted document
public ApexPages.Pagereference upload()
 {

    //This shows how to insert an Attachment
    attach.ParentId = parentId;
    insert attach;

    return new ApexPages.Standardcontroller(attach).view();   
  }
 }

//AttachmentUpload - Visualforce Page
<apex:page controller="AttachmentUploadController">
<apex:form >
    <apex:outputText value="Parent Object ID: "/><apex:inputText value="   {!parentId}"/><br/>
    <apex:outputText value="Input File:  "/><apex:inputFile value="{!attach.body}"   filename="{!attach.name}"/><br/>
    <apex:commandButton value="Upload" action="{!upload}"/>
</apex:form>

Please find the above code to help you understand .

The wrong thing is i see you are inserting record twice .If you are updating parent record its update .

  • To help clarify - the attachment in the code I posted was intended for a new case. – jlim Mar 28 '13 at 20:59
  • Since this is the only answer, I will mark it as the answer since it helped me resolve this. Thank you. – jlim Mar 28 '13 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.