public class UploadContractAttachmentController {
public String selectedType {get;set;}
public String description {get;set;}
private Contract contract {get;set;}
public String fileName {get;set;}
public Blob fileBody {get;set;}
public UploadContractAttachmentController(ApexPages.StandardController controller) {
this.contract = (Contract)controller.getRecord();
}
// creates a new Contract_Attachment__c record
private Database.SaveResult saveCustomAttachment() {
Contract_Attachment__c obj = new Contract_Attachment__c();
obj.contract__c = contract.Id;
obj.description__c = description;
obj.type__c = selectedType;
// fill out cust obj fields
return Database.insert(obj);
}
// create an actual Attachment record with the Contract_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;
// inser the attahcment
result = Database.insert(attachment);
// reset the file for the view state
fileBody = Blob.valueOf(' ');
return result;
}
/**
* Upload process is:
* 1. Insert new Contract_Attachment__c record
* 2. Insert new Attachment with the new Contract_Attachment__c record as parent
* 3. Update the Contract_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
Contract_Attachment__c customAttachment = [select id from Contract_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('/'+contract.Id);
}
public PageReference back() {
return new PageReference('/'+contract.Id);
}
}
Hi David, try the following test class @isTest
public class UploadContractAttachmentController_Tests {
@isTest static void testProcessUpload(){
// set the page
test.setCurrentPageReference(page.UploadContractAttachment);
//create the contract here
contract c = new contract();
c.Status = 'Draft';
c.StartDate = system.today();
c.ContractTerm = 12;
insert c;
ApexPages.StandardController sc = new ApexPages.StandardController(c);
UploadContractAttachmentController ucac = new UploadContractAttachmentController(sc);
//fill page variables
ucac.selectedType = 'VALUE';
ucac.description = 'VALUE';
ucac.fileName = 'FILE NAME';
ucac.fileBody = blob.valueOf('abcd');
test.startTest();
//click the button
ucac.processUpload();
test.stopTest();
}
}