You need to sign in to do that
Don't have an account?
shobana shobana
Provision to attach a new attachment!!!
Hi everyone,
I need to upload new attachment to opportunity,
If I click on New Attachment button( first Visualforce page ) , it redirects to other Visualforce and provision to choose file and click upload, then the attachment appears in first Visualforce page.
I am not getting any error but functionality is not working..
Actually when i am clicking on Choosefile button on second visualforce page we can choose a file but the thing is while am clicking the upload button i want to show the chosen file in first visualforce.
How do I do that?
I wrote some code below, and I attached screenshots below.
First visualfore page:
My Second visualforce page:
Thank you in advance...
I need to upload new attachment to opportunity,
If I click on New Attachment button( first Visualforce page ) , it redirects to other Visualforce and provision to choose file and click upload, then the attachment appears in first Visualforce page.
I am not getting any error but functionality is not working..
Actually when i am clicking on Choosefile button on second visualforce page we can choose a file but the thing is while am clicking the upload button i want to show the chosen file in first visualforce.
How do I do that?
I wrote some code below, and I attached screenshots below.
First visualfore page:
My Second visualforce page:
my visualfore page-1 <apex:page standardController="opportunity" extensions="FileUploadController"> <apex:form > <apex:pageblock > <apex:pageBlockSection title="Attachments" columns="1"> <apex:CommandButton value="New Attachment" action="{!attach_new}"/> </apex:pageBlockSection> </apex:pageblock> </apex:form> </apex:page> Visualforce page-2 <apex:page standardController="Opportunity" extensions="FileUploadController" showHeader="true"> <apex:pageBlock id="Pid"> <apex:pageBlockSection title="Select a file to be uploaded" collapsible="false"/> </apex:pageBlock> <apex:outputPanel id="panel_Upload"> <apex:form id="form_Upload"> <apex:inputFile id="file_File" value="{!fileBody}" filename="{!fileName}"/> <apex:actionRegion > <apex:commandButton id="button_Upload" value="Upload" action="{!processUpload}"/> </apex:actionRegion> <apex:CommandButton value="Cancel" action="{!Cancel}"/> </apex:form> </apex:outputPanel> <apex:pageBlock id="BId"> <apex:pageBlockSection > <apex:dataTable Value="{!la}" var="l"> <apex:column value="{!l.Name}"/> </apex:dataTable> </apex:pageBlockSection> </apex:pageBlock> </apex:page> My controller: public class FileUploadController { Public Opportunity opp{get; set;} private String fileName; private Integer fileSize; private Blob fileBody; public FileUploadController(ApexPages.StandardController controller) { } public String getFileName() { return this.fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public Blob getFileBody() { return this.fileBody; } public void setFileBody(Blob fileBody) { this.fileBody = fileBody; setFileSize(this.fileBody.size()); } public Integer getFileSize() { return this.fileSize; } public void setFileSize(Integer fileSize) { this.fileSize = fileSize; } List<Attachment> la{get;set;} Public List<Attachment> getLa(){ return la; } private Database.SaveResult insertCustomObject() { Opportunity custObj = new Opportunity(); custObj.Name= 'Ranjith'; insert la; return Database.insert(custObj); } private Database.SaveResult insertAttachment(Id parentId) { Database.SaveResult result; Attachment attachment = new Attachment(); attachment.Body = this.fileBody; attachment.Name = this.fileName; attachment.ParentId = parentId; result = Database.insert(attachment); fileBody = Blob.valueOf(' '); return result; } public Void processUpload() { try { Database.SaveResult result = insertCustomObject(); result = insertAttachment(result.getId()); } catch (Exception e) { ApexPages.AddMessages(e); } } public pagereference attach_new(){ pagereference pag = new pagereference('/apex/NewAttachments'); return pag; } }Anyone help me out solve this scenario.
Thank you in advance...
There are many mistakes you were doing in the code but you were not able to see them because they were coming in debug log.
Let me summarize some issues first which your code have :
1. Close date and stagename is always required while creating the opportunity. those were missing at line 80
2. Until and unless we don't have any parent object for attachment. attachement can't be inserted.
3. There was no code written to redirect back on you fileupload page.
4. Action region was used on page.
Here is the rectified version of your code Please have a look and try to understand the code
File Uplaod VF page
NewAttachment VF Page
Controller:
Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
All Answers
There are many mistakes you were doing in the code but you were not able to see them because they were coming in debug log.
Let me summarize some issues first which your code have :
1. Close date and stagename is always required while creating the opportunity. those were missing at line 80
2. Until and unless we don't have any parent object for attachment. attachement can't be inserted.
3. There was no code written to redirect back on you fileupload page.
4. Action region was used on page.
Here is the rectified version of your code Please have a look and try to understand the code
File Uplaod VF page
NewAttachment VF Page
Controller:
Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thank you for giving me reply.Its working good.
But the thing before showing the result it shows
Actually i have changed my class as fileupload.
Can you please explain why am getting this error.
at line 75 in controller replace pageupload with your first pagename. I was using fileupload as vf page name.
Thanks,
Himanshu
Himanshu Parashar
Thank you for giving reply and its working good...........
For the same concept i want to show the checkbox and remove button in my first visualforce page
in order to remove the attachments.for that i used wrapper class concept.But i took a seperate method for remove action.How to show both remove action and attachments in single pageblock table.
Just give me guidelines..
Thank you in advance..