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
Jonathan91Jonathan91 

Basic case controller with attachment

I'm new to SF and trying to write a basic 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 see the attachment

when I view a new case under the contact.

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>
Best Answer chosen by Admin (Salesforce Developers) 
Cory CowgillCory Cowgill

Take a look at this blog post I wrote a long time back. Hope this helps.

 

http://corycowgill.blogspot.com/2011/01/i-see-lot-of-folks-posting-questions-in.html

All Answers

Cory CowgillCory Cowgill

Take a look at this blog post I wrote a long time back. Hope this helps.

 

http://corycowgill.blogspot.com/2011/01/i-see-lot-of-folks-posting-questions-in.html

This was selected as the best answer
BobBob

Do you have a test method for this controller?