You need to sign in to do that
Don't have an account?
Carlos Naranjo
Code Structure.
Hello all,
Just a question and some guidance.
I have this code and the Visualforce page that goes with it. I was wondering if the structure is good enough of if there are things that could be improve.
The code works well and all but I'm sure it could be better, so any tips on it will be very much welcome.
Thanks.
This the controller:
public with sharing class AttachmentController {
String attachId;
public AttachmentController(ApexPages.StandardController controller) {
attachId = controller.getId();
}
public Attachment attachment {
get {
if (attachment == null)
attachment = new Attachment();
return attachment;
}
set;
}
public PageReference save() {
attachment.OwnerId = UserInfo.getUserId();
attachment.ParentId = ApexPages.currentPage().getParameters().get('id');
try {
insert attachment;
} catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
return null;
}
finally {
attachment = new Attachment();
}
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
return null;
}
public String getFileId() {
String fileId = '';
List<Attachment> attachedFiles = [select Id from Attachment where parentId =:attachId order By LastModifiedDate DESC limit 1];
if( attachedFiles != null && attachedFiles.size() > 0 ) {
fileId = attachedFiles[0].Id;
}
return fileId;
}
}
This is the visualforce page:
<apex:page StandardController="Whatever__c" extensions="AttachmentController" showHeader="false">
<apex:form id="theForm">
<apex:pageBlock >
<table>
<td>
<apex:image url="/servlet/servlet.FileDownload?file={!FileId}" style="vertical-align:left" width="500" height="550" />
<apex:pageBlockSection >
<apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
<apex:commandButton value="Save" action="{!save}" /><br />
</apex:pageBlockSection>
</td>
</table>
</apex:pageBlock>
</apex:form>
</apex:page>
-----------------------------------
Thanks
Just a question and some guidance.
I have this code and the Visualforce page that goes with it. I was wondering if the structure is good enough of if there are things that could be improve.
The code works well and all but I'm sure it could be better, so any tips on it will be very much welcome.
Thanks.
This the controller:
public with sharing class AttachmentController {
String attachId;
public AttachmentController(ApexPages.StandardController controller) {
attachId = controller.getId();
}
public Attachment attachment {
get {
if (attachment == null)
attachment = new Attachment();
return attachment;
}
set;
}
public PageReference save() {
attachment.OwnerId = UserInfo.getUserId();
attachment.ParentId = ApexPages.currentPage().getParameters().get('id');
try {
insert attachment;
} catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
return null;
}
finally {
attachment = new Attachment();
}
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
return null;
}
public String getFileId() {
String fileId = '';
List<Attachment> attachedFiles = [select Id from Attachment where parentId =:attachId order By LastModifiedDate DESC limit 1];
if( attachedFiles != null && attachedFiles.size() > 0 ) {
fileId = attachedFiles[0].Id;
}
return fileId;
}
}
This is the visualforce page:
<apex:page StandardController="Whatever__c" extensions="AttachmentController" showHeader="false">
<apex:form id="theForm">
<apex:pageBlock >
<table>
<td>
<apex:image url="/servlet/servlet.FileDownload?file={!FileId}" style="vertical-align:left" width="500" height="550" />
<apex:pageBlockSection >
<apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
<apex:commandButton value="Save" action="{!save}" /><br />
</apex:pageBlockSection>
</td>
</table>
</apex:pageBlock>
</apex:form>
</apex:page>
-----------------------------------
Thanks
http://www.infallibletechie.com/2013/04/how-to-add-error-message-in-visualforce.html
If this solves your problem, kindly mark it as the best answer.
Regards,
Magulan
http://www.infallibletechie.com
Thnanks.