Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String selectedCAorMA {get;set;}
- public Boolean selectedCAProcessed {get;set;}
- public Boolean selectedMAProcessed {get;set;}
- public String description {get;set;}
- private Refugee__c refugee {get;set;}
- public String fileName {get;set;}
- public Blob fileBody {get;set;}
- public UploadAttachmentController(ApexPages.StandardController controller) {
- this.refugee = (Refugee__c)controller.getRecord();
- }
- // creates a new Refugee_Attachment__c record
- private Database.SaveResult saveCustomAttachment() {
- Refugee_Attachment__c obj = new Refugee_Attachment__c();
- obj.refugee__c = refugee.Id;
- obj.description__c = description;
- obj.CA_or_MA__c = selectedCAorMA;
- obj.CA_Processed__c = selectedCAProcessed;
- obj.MA_Processed__c = selectedMAProcessed;
- // fill out cust obj fields
- return Database.insert(obj);
- }
- // creates an actual Attachment record with the Refugee_Attachment__c as parent
- private Database.SaveResult saveStandardAttachment(Id parentId) {
- Database.SaveResult result;
- Attachment attachment = new Attachment();
- attachment.body = this.fileBody;
- attachment.name = this.fileName;
- attachment.parentId = parentId;
- // insert the attachment
- result = Database.insert(attachment);
- // reset the file for the view state
- fileBody = Blob.valueOf(' ');
- return result;
- }
- /**
- * Upload process is:
- * 1. Insert new Refugee_Attachment__c record
- * 2. Insert new Attachment with the new Refugee_Attachment__c record as parent
- * 3. Update the Refugee_Attachment__c record with the ID of the new Attachment
- **/
- public PageReference processUpload() {
- try {
- Database.SaveResult customAttachmentResult = saveCustomAttachment();
- if (customAttachmentResult == null || !customAttachmentResult.isSuccess()) {
- ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,
- 'Could not save attachment.'));
- return null;
- }
- Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId());
- if (attachmentResult == null || !attachmentResult.isSuccess()) {
- ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,
- 'Could not save attachment.'));
- return null;
- } else {
- // update the custom attachment record with some attachment info
- Refugee_Attachment__c customAttachment = [select id from Refugee_Attachment__c where id = :customAttachmentResult.getId()];
- customAttachment.name = this.fileName;
- customAttachment.Attachment__c = attachmentResult.getId();
- update customAttachment;
- }
- } catch (Exception e) {
- ApexPages.AddMessages(e);
- return null;
- }
- return new PageReference('/'+Refugee.Id);
- }
- public PageReference back() {
- return new PageReference('/'+Refugee.Id);
- }
Add Comment
Please, Sign In to add comment