• David Kassouf
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 7
    Replies
Hello All,

I am sure you see requests all the time to aid in the creation, modification and testing of new APEX code from greenhorns like myself. The code below is complete and helps with a custom attachment object that replaces the standard Notes & Attachments section on Contracts. The code, object and process works great in the Sandbox environment but I am having difficulties testing the code.

I have browsed through the forums looking and have been researching online for best practices on testing APEX coding in general and have been unable to tackle the project fully. If anyone could assist, I would greatly appreciated it!

User-added image
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);
    }     
 
}

 
Hello All,

I am sure you see requests all the time to aid in the creation, modification and testing of new APEX code from greenhorns like myself. The code below is complete and helps with a custom attachment object that replaces the standard Notes & Attachments section on Contracts. The code, object and process works great in the Sandbox environment but I am having difficulties testing the code.

I have browsed through the forums looking and have been researching online for best practices on testing APEX coding in general and have been unable to tackle the project fully. If anyone could assist, I would greatly appreciated it!

User-added image
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);
    }     
 
}