You need to sign in to do that
Don't have an account?
LIM 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); } } }
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:-
In case you find any other issue please mention.
If you find your Solution then mark this as the best answer.
All Answers
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:-
In case you find any other issue please mention.
If you find your Solution then mark this as the best answer.
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.
Can you tell me....what is the error in debug log?
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