You need to sign in to do that
Don't have an account?
SMaster
Using Visualforce Page.. how to upload file in Notes & Attachments
Hi All,
In the custom object.. i want to upload a file inside Notes & Attachments using the visualforce page.. please let me know how can i do that....
just use Trigger.New instead of Trigger.New()
All Answers
Hi
You can use
<apex:inputFile value="{!File}" filename="{!FileNameToAttach}" contentType="Type of File" id="inputFile" title="Type the path of the file or click the Browse button to find the file."/>
On your VF Page
on Apex class use like .......
Attachment myAttachment = new Attachment();
Hi virendra,
Unfortunately i have no idea to implement this logic... i was using totally different approach...
My Custom Object name is > opportunity_proposals__c
Please provide me the code snippet for the apex and visualforce page... i'll really be thankful...
hello Sir,
Based on the suggestion provided by you I am using VF page for this... and created a apex class to upload the file inside Notes & attachments...
public class PropUpload
{
public Id recId
{ get;set; }
public PropUpload(ApexPages.StandardController ctlr)
{
recId = ctlr.getRecord().Id;
}
public string fileName
{ get;set; }
public Blob fileBody
{ get;set; }
public PageReference UploadFile()
{
PageReference pr;
if(fileBody != null && fileName != null)
{
Attachment myAttachment = new Attachment();
myAttachment.Body = fileBody;
myAttachment.Name = fileName;
myAttachment.ParentId = recId;
insert myAttachment;
PageReference page = ApexPages.currentPage();
page.setRedirect(true);
return page;
}
return null;
}
}
and i have created the following visualforce page;
<apex:page standardController="opportunity_proposals__c" extensions="PropUpload">
<apex:form >
<div id="upload" class="upload">
<table><tr>
<td><apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/></td>
<td><apex:commandButton value="Upload Attachment" action="{!uploadFile}" styleClass="upload-button"/></td>
</tr></table>
</div>
</apex:form>
</apex:page>
but its not working please confirm... where did i commited a mistake..
Hi
are you able to open browse window to select file.
Try contentType Like
<apex:inputFile value="{!FileBody}" filename="{!FileName}" contentType="{!contentType}" id="inputFile" title="Type the path of the file or click the Browse button to find the file."/>
In controller
save Attachments with contentType
newAttachment.ContentType = contentTypeFile;
Hello sir,
I am able to see Browse window to select file.. but after selecting the file as i click on the upload button i get the following error;
"
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent]: [Parent]
Class.PropUpload.UploadFile: line 26, column 11 External entry point "
if possible please suggest the code changes.. and provide the modified code... please
Please make sure that
For
myAttachment.ParentId = recId;
Check recId is not null and
recId should be valid Sobject Id
sir,
how can i know that.... i am new in this.. please suggest
Add <apex:messages/> Tag to your VF Page
Add Following lines to Your Controller UploadFile Method
Perfectly done Sir... Now i am able to upload the file sucessfully inside Notes & Attachments...
Now.. Sir another important thing to implement in this.. on the page layout i have a custom button and after clicking on that button..a field will be updated... and the comlete record along with Notes and attachments will be freezed..... how can i achieve that... please guide..
write a trigger to update the Field of custom object
rest things will be as it is ...
Sir,
Please provide the code to update the field with some value after attachment is saved...
My Custom Object Name is opportunity_proposals__c and custom field name is Proposal_Status__c with data type as text...
trigger updateFiendsForObject on opportunity_proposals__c (before update){
for(opportunity_proposals__c oppProp: Trigger.New()){
oppProp.Proposal_Status__c = "Update Value";
}
}
Sir,
it is giving Compile Error: Method does not exist or incorrect signature: Trigger.New() at line 3
trigger updateFiendsForObject on opportunity_proposals__c (before update)
{
for(opportunity_proposals__c oppProp: Trigger.New())
{
oppProp.Proposal_Status__c = 'Sent';
}
}
Its done sir... Trigger is working fine... can it be done on button click... i mean how i update the field on button click...
just use Trigger.New instead of Trigger.New()
Hello Sir,
Please suggest the solution to display all the attachments on Visualforce page...
Hi
1.Query for all the attachment where ParentId of attachment is equal to your sObject
2.It will give you the list of Attachment
3.Use page BlockTable to display all the attachments of vf Page
Hello Sir,
i am using the following code
public class PropUpload
{
public Id recId
{ get;set; }
public PropUpload(ApexPages.StandardController ctlr)
{
recId = ctlr.getRecord().Id;
}
public string fileName
{ get;set; }
public Blob fileBody
{ get;set; }
public PageReference UploadFile()
{
PageReference pr;
if(fileBody != null && fileName != null)
{
Attachment myAttachment = new Attachment();
myAttachment.Body = fileBody;
myAttachment.Name = fileName;
myAttachment.ParentId = recId;
insert myAttachment;
PageReference page = ApexPages.currentPage();
page.setRedirect(true);
return page;
}
return null;
}
}
and i have created the following visualforce page;
<apex:page standardController="opportunity_proposals__c" extensions="PropUpload">
<apex:form >
<div id="upload" class="upload">
<table><tr>
<td><apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/></td>
<td><apex:commandButton value="Upload Attachment" action="{!uploadFile}" styleClass="upload-button"/></td>
</tr></table>
</div>
</apex:form>
</apex:page>
please make the changes in the code that are required.. to display the uploaded attachments.. on visualforce page....