I'm trying to create a few triggers that prevent most of our org from deleting various items in our cases and opportunities (such as activities, case comments, etc.) I have been able to create the triggers for all of the ones I need, and the test classes for all have received 100% coverage, except for the one I have made for Attachments. I'm unsure as to what's so special about the Attachment object that's preventing me from getting the code coverage on this one as well, and it's preventing me from moving all these triggers to production. Here is the trigger I made to prevent the deletion of attachments:

    trigger NoDeleteonAttachment on Attachment (before delete)
       String ProfileId = UserInfo.getProfileId();  
       List<Profile> profiles=[select id from Profile where name='System Administrator' or name='Management'];

       if (2!=profiles.size())
           for (Attachment a : Trigger.old)      
              if ( (profileId!=profiles[0].id) || (profileId!=profiles[1].id) )
                 a.addError('You can\'t delete this record!');

This should prevent any user that doesn't have the profile of 'System Administrator' or 'Management' from deleting the Attachment. Below is my test class:

    private class NoDeleteonAttachmentTest 
        private static testmethod void NoDeleteonAttachmentTest()
               User U = [select id from User where name='Sam Jones'];

               Account Acct = new Account(Name = 'Test');
               insert Acct;

               contact cont = new contact(Accountid = Acct.id, LastName='Test');
               insert cont;

               Task a=new Task(Ownerid = U.id, Subject = 'Test', Whoid = cont.id, Whatid = Acct.id, Status = 'Pending', Priority = 'Low', Description = 'Test');
               insert a;

               Case b = new Case();
               insert b;

               a.WhoId = b.Id;

               Attachment attachTest = new Attachment(Name='Test');
               attachTest.body = Blob.valueOf('');
               attachTest.ParentID = b.Id;
               attachTest.OwnerID = U.Id;
               insert attachTest;

               delete attachTest;



When I run the test, I get the error, System.DmlException: Delete failed. First exception on row 0 with id 00Pg0000001CCfTEAW; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You can delete this record!:[]

Sam Jones (the user used in this test) is a user without the access required to be able to delete the attachment. Thus, the test runs correctly, it can't complete because he doesn't have the access to do this. My coverage stays at 0% however.

In my other tests, I get an error similar to this, and end up getting 100% coverage. Does anyone know what the hiccup is with the attachment object? Thanks in advance!

  • Where does it say you have 0% covered? Are you looking online or in eclipse? – dphil Feb 28 '14 at 18:49
  • I'm implementing these in our sandbox org prior to putting them in production, so I'm not using Eclipse for these yet. The 0% coverage is from looking at the trigger in the sandbox. – grimsvotn Feb 28 '14 at 18:52
  • I don't really understand why you say you aren't using Eclipse for them 'yet'. Have the test run in Eclipse. Online measurements are often bugged. – dphil Feb 28 '14 at 18:53

Your trigger is successfully preventing the deletion and the addError on the trigger is causing the whole transaction to rollback. In this scenario, you have to wrap your delete DML in a try/catch block and then test whether or not you receive the custom error that you threw. Instead of

delete attachTest;

Try this:

try {
    delete attachTest;
} catch (Exception e) {
    system.assertEquals('You can\'t delete this record!', e.getMessage());
  • Thanks for this. It now changed to a different error, however and I'm still at 0% coverage. System.AssertException: Assertion Failed: Expected: You can't delete this record!, Actual: Delete failed. First exception on row 0 with id 00Pg0000001CCgMEAW; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You can delete this record!: [] – grimsvotn Feb 28 '14 at 18:44

In your scenario there is a nice method available: System.runAs(userReference).

  • Create all information in normal context. Administrator profile.

  • when u delete pass the user "sam jones" in System.runAs(u).

System.runAs(U) // same jones { try { Delete attachtest; } catch(Exception e) {} }

  • Please format code. I am unable to do this. Thanks! – Ashwani Feb 28 '14 at 18:39
  • 1
    FYI, just highlight the code and Ctrl+K, or use the Format Code button on the toolbar. – sfdcfox Mar 2 '14 at 3:19
  • @sfdcfox. Thanks. I didn't know about ctrl+k. I used format code button but format code didn't work from a cellphone. – Ashwani Mar 4 '14 at 10:10

The online code measurements are often buggy. It's not a good idea to do your development and tests directly online. I recommend using the Eclipse plugin for SFDC. You can easily run your tests there and it will give you details for specific lines that aren't getting hit.



Have you tried clearing your code test coverage in the Apex Test Execution section?

There have been some issues with the legacy format of code coverage that led to unpredictable results that may be at play here. Clearing the test history removes all the data, including the old format that can cause these issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.