Here is the code for creating the opportunity attachment controller object in Apex.
From the developer console choose file new.
Select Apex Class and insert the code.
Save the file as UploadAttachmentController.apxc
public class UploadAttachmentController {
public String selectedType {get;set;}
public String subject {get;set;}
public Boolean selectedComit {get;set;}
private Contact contact {get;set;}
public String fileName {get;set;}
public Blob fileBody {get;set;}
public UploadAttachmentController(ApexPages.StandardController controller) {
this.contact = (Contact)controller.getRecord();
}
// creates a new Contact_Attachment__c record
private Database.SaveResult saveCustomAttachment() {
Contact_Attachment__c obj = new Contact_Attachment__c();
obj.contact__c = contact.Id;
obj.subject__c = subject;
obj.type__c = selectedType;
obj.comit__c = selectedComit;
// fill out custom obj fields
return Database.insert(obj);
}
// create an actual Attachment record with the Contact_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 attahcment
result = Database.insert(attachment);
// reset the file for the view state
fileBody = Blob.valueOf(' ');
return result;
}
/*******************************************************************************
* Upload process is:
* 1. Insert new Contact_Attachment__c record
* 2. Insert new Attachment with the new Contact_Attachment__c record as parent
* 3. Update the Contact_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
Contact_Attachment__c customAttachment = [select id from Contact_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('/'+contact.Id);
}
public PageReference back() {
return new PageReference('/'+contact.Id);
}
}