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
rtscottrtscott 

Help: Add attachment to Case record using Visualforce "apex:inputFile" component?

Hello,

 

I am building a Visualforce page that is bound to the Case standard controller. This page is replacing the standard New Case page in Salesforce.com. I want to include an input that allows users to add an attachment to their Case when they are creating the record using my page. I already have a controller extension that this page is using to enhance the New Case process. I see that there was a new "apex:inputFile" component included in the Winter 09 release that allows me to do this. The documentation gives me the following usage example:

 

(from: http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_inputFile.htm)

 

 

<; Page: --><apex:page standardController="Document" extensions="documentExt"><-- Upload a file and put it in your personal documents folder--> <apex:messages /> <apex:form id="theForm"> <apex:pageBlock> <apex:pageBlockSection> <apex:inputFile value="{!document.body}" filename="{!document.name}"/> <apex:commandButton value="save" action="{!save}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form></apex:page> /*** Controller ***/public class documentExt { public documentExt(ApexPages.StandardController controller) { Document d = (Document) controller.getRecord(); d.folderid = UserInfo.getUserId(); //this puts it in My Personal Documents } }

 

However, this code assumes that the page is bound to the Document standard controller. Also, since the Case record is not yet created, I cannot specify the parent ID of the Case for my attachment since it does not exist yet (remember, this page replaces the standard New Case page.)

 

Can someone explain to me how I would modify my controller extension to make the "apex:inputFile" component work in this scenario? I'm planning on adding this to my Visualforce page:

 

<apex:pageBlockSectionItem> <apex:outputLabel value="Add attachment:" /> <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" /> <apex:commandButton value="save" action="{!save}" /></apex:pageBlockSectionItem>

 

 

 but I know that I will need to modify my controller extension to make this work. Also, if I use:

 

<apex:commandButton value="save" action="{!save}" />

 

 

how will it know to just save the attachment, and not the entire Case page?

 

Thanks for any help that you can provide. I'm pretty new to Visualforce/Apex development, so examples of how to make this work would be greatly appreciated! :)

 

Thanks,

 

-- Rob 

 

Pault.ax256Pault.ax256

Rob,

 

As you point out, the parent Id of the attachment needs to point to something and you don't have an Id when you are trying to do this.  I have implemented  similar things before (albeit on different objects) but had to provide a button for an attachment that displayed on a detail version of the page.

 

So you would create the Case, hit save and on the detail that displays you would have an inputfile component that lets you add the file.  Would this be acceptable in your scenario ?

 

 

 

 

rtscottrtscott

Thanks for your response.

 

I've been asked to have the "add attachment" button on the New Case form itself, so that a user can select an attachment they want to add to the Case when they are creating the record. I'm wondering if I can set it up so that the user selects the attachment they want to add to the Case, but the attachment is not associated with the ParentID of the Case until they save the record. I imagine that I would have to do this in the controller. If there is no way to do this, I suppose we could just have the 'Add Attachment' button available on the detail record of the Case once it's created, but it would be nice for them to be able to select the attachment while creating the record instead of after it's created. If this is not possible, then I'll just have the 'Add Attachment' section on the detail record, which is standard SFDC functionality.

 

Thanks again for your response,

 

-- Rob 

Pault.ax256Pault.ax256

I reckon that will work mate!  If in your 'save' method you first create an attachment and put in in (say My Documents) and keep the id, you could then create your case, get the Id of that and then change the parent Id of your attachment.

 

If you do a search for 'inputfile' in the dev guide, it gives a nice little example of the controller to create in My Documents.

 

Good Luck with it !

XactiumBenXactiumBen

You will probably need to use a custom controller for this - create your Case record and your attachment record in your own custom controller then have a save function that would save the case record.  Use the Case Id you get in your attachment record then insert this after.  Something like this:

 

public class myCustomController { public Case myCase {get; set;} public Attachment myAttachment {get;set;} public myCustomController() { myCase = new Case(); myAttachment = new Attachment(); } public void save() { insert myCase; myAttachment.ParentId = myCase.Id; insert myAttachment; } }

 

Hope this helps.

PamSalesforcePamSalesforce

Hi,

 

I am trying to achieve something similar with a custom object. Could you plz send me the VF anf controller code for the same.

 

Thanks for the help,

Ezio_2010Ezio_2010

Your solution's good but...i dont know why it doesnt work...I got this...

 

 

 



public class DependentObjects {

public PruebaObj__c c{get;set;}
public Attachment myAttachment {get;set;}

/* String value for the category */
String category;

/* String value for the feature */
String feature;

/* Getter for the category value */
public String getCategory() { return this.category; }

/* Setter for the category value */
public void setCategory(String s) { this.category = s; }

/* Getter for the feature value */
public String getFeature() { return this.feature; }

/* Setter for the feature value */
public void setFeature(String s) { this.feature = s; }


/* Getter which dynamically generates the categories from the Feature_Category__c object. */
public List getCategories() {
List optionList = new List();
/* Add a null option to force the user to make a selection. */
optionList.add(new SelectOption('','- None -'));

/* Loop through the feature_category__c records creating a selectOption
for each result with the record ID as the value and the name as the label
displayed in the selectList */
for (Feature_Category__c fc : [select name from Feature_Category__c order by Name]){
optionList.add(new SelectOption(fc.id,fc.name));
}
return optionList;
}


/* Getter which generates the options for the features selectList based on the current
value of the selected category. If there is no value selected then only
the null option should be returned. */

public List getFeatures() {
List optionList = new List();
/* Add a null option to force the user to make a selection. */
optionList.add(new SelectOption('', '- None -'));

/* If a category has been selected then query for the related values */
if(category != NULL) {

/* Loop over the related feature records for the given category
creating a selectOption with the value being the feature record ID
and the label is the name of the feature. */
for (Feature__c f : [select FeatureName__c from Feature__c f where f.Feature_Category__c = :category]){
optionList.add(new SelectOption(f.id,f.FeatureName__c));
}
}
return optionList;
}

public void salvar() {
insert c;

myAttachment.ParentId = c.Id;
insert myAttachment;
}

public DependentObjects() {
c = new PruebaObj__c();
myAttachment = new Attachment();
}

}



 

and my visualforce page of doom...I got a prueba object inputs texts, and also this...

 

<code>

apex : inputFile value="{!myAttachment.body}" filename="{!myAttachment.name}" 

 </code>




But when i select something in the select component, it just refresh to a blank page...:( i dont even know how to debug the custom controller...
any help?