function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bryan Jimenez 5Bryan Jimenez 5 

dml exception help

Hi Everyone,

I am having trouble getting coverage for my dml exception.
 
global with sharing class ModalFlowAttachment{

    global Flow.Interview.attachmentFlow2 myflow { get; set; }
    global string varAttachmentParentId = 'init';

    global ModalFlowAttachment(){
        myflow  = new Flow.Interview.attachmentFlow2(new map<string, object>{'varAttachmentParentId'=> ''});
    }
    global string getVarAttachmentParentId() {
        if(varAttachmentParentId == 'init'){
            varAttachmentParentId = '';
            return '';
        }
        return myflow.varAttachmentParentId;
    }
    global Attachment attachment {
        get {
            if (attachment == null)
                attachment = new Attachment();
            return attachment;
        }
        set;
    }
    global PageReference upload() {

        attachment.OwnerId = UserInfo.getUserId();
        attachment.IsPrivate = True;
        attachment.parentId = getVarAttachmentParentId();
        
        
        try {
            insert attachment;
        } 
        catch (DMLException e) {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
            return null;
        } 
        finally {
            attachment = new Attachment();
        }

        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
        varAttachmentParentId = null;
        return null;
    }
}

can anyone help me with this?

 
Chris  ByromChris Byrom
You could create a test that doesn't pass in the parentId. If that is possible you will get a DML exception as it is required.
Bryan Jimenez 5Bryan Jimenez 5
Hi Chris,
 
Attached is my Apex Test Class.
 
I tried what you suggested and that still left me at 65% test coverage.
 
@isTest
Private class ModalFlowAttachmentTest {
    
    @testSetup static void CreateProperty() {
        McLabs2__Property__c Property = New McLabs2__Property__c();
        Property.Name                         = 'Fakename';
        Property.McLabs2__Property_Address__c = 'FakeAddress';
        Property.McLabs2__City__c             = 'FakeCity';
        Property.Block__c                     = '328';
        Property.Lot__c                       = '23';
        Insert Property;
    }
   
    static testMethod void ModalFlowAttachment_1() {
    system.Test.setCurrentPage(Page.ModalFlowAttachmentVF);
    ModalFlowAttachment controller = new ModalFlowAttachment();

    controller.getVarAttachmentParentId();
    
    Attachment tmpAttach = controller.attachment;
    Attachment attachment = new Attachment();
    attachment.Body = Blob.valueOf('Dreamforce15');
    attachment.Name = String.valueOf('Coastal Cloud.txt');
    attachment.ParentId = [select id from McLabs2__Property__c limit 1].id;
    
    controller.attachment = attachment;
    controller.upload();

  }
}
after viewing my test class. Is there anything that jumps out at you that I am either missing or doing wrong?