Guest User

Untitled

a guest
May 31st, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. public String selectedCAorMA {get;set;}
  2. public Boolean selectedCAProcessed {get;set;}
  3. public Boolean selectedMAProcessed {get;set;}
  4. public String description {get;set;}
  5. private Refugee__c refugee {get;set;}
  6. public String fileName {get;set;}
  7. public Blob fileBody {get;set;}
  8.  
  9. public UploadAttachmentController(ApexPages.StandardController controller) {
  10. this.refugee = (Refugee__c)controller.getRecord();
  11. }
  12.  
  13. // creates a new Refugee_Attachment__c record
  14. private Database.SaveResult saveCustomAttachment() {
  15. Refugee_Attachment__c obj = new Refugee_Attachment__c();
  16. obj.refugee__c = refugee.Id;
  17. obj.description__c = description;
  18. obj.CA_or_MA__c = selectedCAorMA;
  19. obj.CA_Processed__c = selectedCAProcessed;
  20. obj.MA_Processed__c = selectedMAProcessed;
  21. // fill out cust obj fields
  22. return Database.insert(obj);
  23. }
  24.  
  25. // creates an actual Attachment record with the Refugee_Attachment__c as parent
  26. private Database.SaveResult saveStandardAttachment(Id parentId) {
  27. Database.SaveResult result;
  28.  
  29. Attachment attachment = new Attachment();
  30. attachment.body = this.fileBody;
  31. attachment.name = this.fileName;
  32. attachment.parentId = parentId;
  33. // insert the attachment
  34. result = Database.insert(attachment);
  35. // reset the file for the view state
  36. fileBody = Blob.valueOf(' ');
  37. return result;
  38. }
  39.  
  40. /**
  41. * Upload process is:
  42. * 1. Insert new Refugee_Attachment__c record
  43. * 2. Insert new Attachment with the new Refugee_Attachment__c record as parent
  44. * 3. Update the Refugee_Attachment__c record with the ID of the new Attachment
  45. **/
  46. public PageReference processUpload() {
  47. try {
  48. Database.SaveResult customAttachmentResult = saveCustomAttachment();
  49.  
  50. if (customAttachmentResult == null || !customAttachmentResult.isSuccess()) {
  51. ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,
  52. 'Could not save attachment.'));
  53. return null;
  54. }
  55.  
  56. Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId());
  57.  
  58. if (attachmentResult == null || !attachmentResult.isSuccess()) {
  59. ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,
  60. 'Could not save attachment.'));
  61. return null;
  62. } else {
  63. // update the custom attachment record with some attachment info
  64. Refugee_Attachment__c customAttachment = [select id from Refugee_Attachment__c where id = :customAttachmentResult.getId()];
  65. customAttachment.name = this.fileName;
  66. customAttachment.Attachment__c = attachmentResult.getId();
  67. update customAttachment;
  68. }
  69.  
  70. } catch (Exception e) {
  71. ApexPages.AddMessages(e);
  72. return null;
  73. }
  74.  
  75. return new PageReference('/'+Refugee.Id);
  76. }
  77.  
  78. public PageReference back() {
  79. return new PageReference('/'+Refugee.Id);
  80. }
Add Comment
Please, Sign In to add comment