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
LIM AI KHOONLIM AI KHOON 

Code Coverage give 0%, kindly help

Hi, kindly assists me to fix my coverage code. My scenario is once users submit a record(parent record), they cannot delete/edit the child record as well. Only the admin can edit or delete. 
trigger preventDelete on ChildRecord__c (before delete) {
    
    Id profileId=userinfo.getProfileId();
    String profileName=[Select Name from Profile where Id=:profileId].Name;
    
    Set < ID > testentry = New Set < ID > ();
    
    for (ChildRecord__c tst: trigger.old) {
        testentry.add(tst.ParentRecord__c );
    }
    
    set<id> mdrRecord = new map<id,ParentRecord__c>([Select Id From ParentRecord__c Where (Approval_Status__c= 'pending' OR Approval_Status__c LIKE '%Approved by%') and Id IN: testentry]).keyset();
    
    for (ChildRecord__c record : trigger.old){
        if (!FeatureManagement.checkPermission('Admin_Can_Delete')){
            
            If(mdrRecord.contains(record.ParentRecord__c)){
                record.addError('This record is locked. If you need to delete it, contact your admin.');
            } 
        }
    }
}
Here is my code coverage. I am not sure why it gives me 0% coverage. 
@isTest (SeeAllData=false)
public class preventDeleteTest {
    
    @isTest
    public static void DeletetestPositive(){
        try {            
            Test.startTest();            
            Profile prof = [SELECT Id FROM Profile WHERE Name = 'Marketer']; //get a profile Id
            User testUser = new User(Alias = 'SLYEODDNG', Email = 'slyeoddng@ve.com.invalid', EmailEncodingKey = 'UTF-8', FirstName = 'YEODDNG', LanguageLocaleKey = 'en_US', LastName = 'SUET LESEDSS', LocaleSidKey = 'en_MY', ProfileId = prof.Id, TimeZoneSidKey = 'Asia/Kuala_Lumpur', Username = 'slyeong@ve.com.devcr'); 
            insert testUser;
            System.runAs(testUser) {
                
                ChildRecord__c MDRLineItem = new ChildRecord__c();
                MDRLineItem.Option__c = 'OEerfrMi';
                MDRLineItem.Product__c = 'Nitrilffee PF Polymer';
                MDRLineItem.Packing_Style__c = '4ff3';
                MDRLineItem.Brand_Name__c = 'gggkkjhk';
                MDRLineItem.Brand_Name_OBM__c= '- N/A -';
                MDRLineItem.Weight_Code__c = 'ENW035';
                MDRLineItem.Colour__c = 'BRN frefBlue';
                MDRLineItem.Flavour__c= 'Leerfmon';
                MDRLineItem.Length__c= '270mm';
                MDRLineItem.Surface__c= 'Double Embossed';
                MDRLineItem.Size__c= 'XrefS';
                MDRLineItem.Manufacturer_OEMi_OBM__c= 'Senterfienxerferfe Sdn.Bhd.';
                MDRLineItem.CE_Cert_Number_OEMi_OBM__c= '- N/A -';
                MDRLineItem.Notify_Body_OEMi_OBM__c= '- N/A -';

                insert MDRLineItem;
                Delete MDRLineItem;
                
                
                //Opportunity createOpp = new Opportunity();
                
                
                //Apex Trigger addError Checking
                ChildRecord__c testResult = [SELECT Id, IsDeleted FROM ChildRecord__c WHERE Id =: MDRLineItem.Id];
                system.debug('testResult.IsDeleted:::'+testResult.IsDeleted);
                system.assertEquals(true, testResult.IsDeleted);
            }
            Test.stopTest();
            
        } catch(Exception e) {
            system.debug('error::'+e);
        }
        
    }
}
Best Answer chosen by LIM AI KHOON
Suraj Tripathi 47Suraj Tripathi 47
Hi LIM

Check you alias name in your test class because salesforce provide up to eight characters are allowed in this field and you have to provide 9 characters.

You can take reference from this test class:-
@isTest (SeeAllData=false)
public class preventDeleteTest {
    
    @isTest
    public static void DeletetestPositive(){
        try {            
            Test.startTest();            
            Profile prof = [SELECT Id FROM Profile WHERE Name = 'Marketer']; //get a profile Id
            User testUser = new User(Alias = 'SLYEOD', Email = 'slyeoddng@ve.com.invalid', EmailEncodingKey = 'UTF-8', FirstName = 'YEODDNG', LanguageLocaleKey = 'en_US', LastName = 'SUET LESEDSS', LocaleSidKey = 'en_MY', ProfileId = prof.Id, TimeZoneSidKey = 'Asia/Kuala_Lumpur', Username = 'slyeong@ve.com.devcr'); 
            insert testUser;
            System.runAs(testUser) {
                
                ChildRecord__c MDRLineItem = new ChildRecord__c();
                MDRLineItem.Option__c = 'OEerfrMi';
                MDRLineItem.Product__c = 'Nitrilffee PF Polymer';
                MDRLineItem.Packing_Style__c = '4ff3';
                MDRLineItem.Brand_Name__c = 'gggkkjhk';
                MDRLineItem.Brand_Name_OBM__c= '- N/A -';
                MDRLineItem.Weight_Code__c = 'ENW035';
                MDRLineItem.Colour__c = 'BRN frefBlue';
                MDRLineItem.Flavour__c= 'Leerfmon';
                MDRLineItem.Length__c= '270mm';
                MDRLineItem.Surface__c= 'Double Embossed';
                MDRLineItem.Size__c= 'XrefS';
                MDRLineItem.Manufacturer_OEMi_OBM__c= 'Senterfienxerferfe Sdn.Bhd.';
                MDRLineItem.CE_Cert_Number_OEMi_OBM__c= '- N/A -';
                MDRLineItem.Notify_Body_OEMi_OBM__c= '- N/A -';

                insert MDRLineItem;
                Delete MDRLineItem;
                
                
                //Opportunity createOpp = new Opportunity();
                
                
                //Apex Trigger addError Checking
                ChildRecord__c testResult = [SELECT Id, IsDeleted FROM ChildRecord__c WHERE Id =: MDRLineItem.Id];
                system.debug('testResult.IsDeleted:::'+testResult.IsDeleted);
                system.assertEquals(true, testResult.IsDeleted);
            }
            Test.stopTest();
            
        } catch(Exception e) {
            system.debug('error::'+e);
        }
        
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi LIM

Check you alias name in your test class because salesforce provide up to eight characters are allowed in this field and you have to provide 9 characters.

You can take reference from this test class:-
@isTest (SeeAllData=false)
public class preventDeleteTest {
    
    @isTest
    public static void DeletetestPositive(){
        try {            
            Test.startTest();            
            Profile prof = [SELECT Id FROM Profile WHERE Name = 'Marketer']; //get a profile Id
            User testUser = new User(Alias = 'SLYEOD', Email = 'slyeoddng@ve.com.invalid', EmailEncodingKey = 'UTF-8', FirstName = 'YEODDNG', LanguageLocaleKey = 'en_US', LastName = 'SUET LESEDSS', LocaleSidKey = 'en_MY', ProfileId = prof.Id, TimeZoneSidKey = 'Asia/Kuala_Lumpur', Username = 'slyeong@ve.com.devcr'); 
            insert testUser;
            System.runAs(testUser) {
                
                ChildRecord__c MDRLineItem = new ChildRecord__c();
                MDRLineItem.Option__c = 'OEerfrMi';
                MDRLineItem.Product__c = 'Nitrilffee PF Polymer';
                MDRLineItem.Packing_Style__c = '4ff3';
                MDRLineItem.Brand_Name__c = 'gggkkjhk';
                MDRLineItem.Brand_Name_OBM__c= '- N/A -';
                MDRLineItem.Weight_Code__c = 'ENW035';
                MDRLineItem.Colour__c = 'BRN frefBlue';
                MDRLineItem.Flavour__c= 'Leerfmon';
                MDRLineItem.Length__c= '270mm';
                MDRLineItem.Surface__c= 'Double Embossed';
                MDRLineItem.Size__c= 'XrefS';
                MDRLineItem.Manufacturer_OEMi_OBM__c= 'Senterfienxerferfe Sdn.Bhd.';
                MDRLineItem.CE_Cert_Number_OEMi_OBM__c= '- N/A -';
                MDRLineItem.Notify_Body_OEMi_OBM__c= '- N/A -';

                insert MDRLineItem;
                Delete MDRLineItem;
                
                
                //Opportunity createOpp = new Opportunity();
                
                
                //Apex Trigger addError Checking
                ChildRecord__c testResult = [SELECT Id, IsDeleted FROM ChildRecord__c WHERE Id =: MDRLineItem.Id];
                system.debug('testResult.IsDeleted:::'+testResult.IsDeleted);
                system.assertEquals(true, testResult.IsDeleted);
            }
            Test.stopTest();
            
        } catch(Exception e) {
            system.debug('error::'+e);
        }
        
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
This was selected as the best answer
LIM AI KHOONLIM AI KHOON
Hello Suraj,

Thank you for your help. I already fix it and try to do test execution, but it still shows 0% code coverage. Is there any other reason? The result for test execution is pass.
Suraj Tripathi 47Suraj Tripathi 47
Hi LIM,

Can you tell me....what is the error in debug log?
Suraj Tripathi 47Suraj Tripathi 47
In my org this test class coverage is 90%
User-added image
LIM AI KHOONLIM AI KHOON
No error in debug log. success. I think it is because of this?
FeatureManagement.checkPermission('Admin_Can_Delete')

When I recheck my code, its looks like I dont use profileName. But I use custom permission. How to do code coverage for that? the permission I put in admin profile
 
Suraj Tripathi 47Suraj Tripathi 47
You have used try catch block that's why it is showing success. You can check message by clicking debug log.
LIM AI KHOONLIM AI KHOON
Got it. It is because of a duplicate username and one required field not fill in. 
MDRLineItem.ParentRecord__c = 'a0V5D000001rW8hUAE';
But when I add this field, it give me error:
13:42:03:381 VARIABLE_ASSIGNMENT [44]|e|"common.apex.runtime.impl.DmlExecutionException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []"|0x7e5a2ba8