You need to sign in to do that
Don't have an account?
Oksana Dovbak
test class for delete attachment trigger
Hello,
Can someone please help me to write a test class for this Apex Trigger -
The following test I have is only giving me 30% code coverage -
Thank you!!!
Can someone please help me to write a test class for this Apex Trigger -
trigger AttachmentSaver_v2 on Attachment (before delete) { // Get the current user's profile name Profile prof = [select Name from Profile where Id = :UserInfo.getProfileId() ]; set<Id> oppSet = new set<Id>(); // If current user is not a System Administrator, do not allow Attachments to be deleted System.debug(Logginglevel.ERROR+'Inside trigger Name '+prof.Name); if (!'System Administrator'.equalsIgnoreCase(prof.Name)) { System.debug(Logginglevel.ERROR+'Inside IF Name '+prof.Name); for (Attachment a : Trigger.old) { oppSet.add(a.PARENTID); } if(oppSet != null){ Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([SELECT Id,Name,StageName FROM Opportunity WHERE ID IN: oppSet]); for (Attachment a : Trigger.old) { if(oppMap.containsKey(a.ParentId) && oppMap.get(a.ParentId).StageName == 'Closed Won') a.addError('You can not delete, Opportunity Stage is '+oppMap.get(a.ParentId).StageName); } } } }
The following test I have is only giving me 30% code coverage -
@isTest private class TestAttachmentSaver { /** * Verify that Standard User profiles are unable to delete attachments */ static testMethod void testStandardUser() { // Create a new user with the Standard User profile Profile standardProf = [select id from profile where name='Standard User']; User su = new User(alias = 'standt', email='standarduser@testorg.com', emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = standardProf.Id, timezonesidkey='America/Los_Angeles', username='standarduser231@testorg.com'); } static testMethod void testAdminUser() { // Next make sure that a System Admin *can* delete an attachment Profile adminProf = [select id from profile where name='System Administrator']; User au = new User(alias = 'admint', email='adminuser@testorg.com', emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = adminProf.Id, timezonesidkey='America/Los_Angeles', username='adminuser123456789@testorg.com'); // Switch current user to System Admin user System.runAs(au) { // Create test data (a new Account with an Attachment) Account acct = new Account(Name = 'Test Account'); insert acct; Blob attachBody = Blob.valueOf('attachment body'); Attachment attach = new Attachment(Name = 'TestAttachment', ParentId = acct.Id, Body = attachBody); insert attach; // Verify that no error is thrown when the attachment is deleted Boolean errorThrown = false; try { delete attach; } catch (Exception e) { System.debug(e); errorThrown = true; } System.assert(!errorThrown); // Switch current user to Standard User } System.runAs(au) { // Create test data (a new Account with an Attachment) Account acct = new Account(Name = 'Test Account'); insert acct; Blob attachBody = Blob.valueOf('attachment body'); Attachment attach = new Attachment(Name = 'TestAttachment', ParentId = acct.Id, Body = attachBody); insert attach; } } }
Thank you!!!
All Answers