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 %

 Hi, can help me with how to have code coverage for this code?
trigger UpdateRecordOwner on Centive__c (before insert, before update) {
    
    Map<Id, User> userMap = new Map<Id, User>([select id, EmployeeNumber from user where (NOT UserRole.name like'IC%') AND (UserRoleId !=NULL)]);
    
    Map<String, Id> mapEmployeeNoToId = new Map<String, Id>();
    for(User u : userMap.values())
        mapEmployeeNoToId.put(u.EmployeeNumber, u.Id);
    for (Centive__c Centive: Trigger.new){
        if(mapEmployeeNoToId.get(Centive.HOT_Badge_ID__c) !=null)
            Centive.OwnerId = mapEmployeeNoToId.get(Centive.HOT_Badge_ID__c);
        System.debug('MapValue'+ mapEmployeeNoToId.get(Centive.HOT_Badge_ID__c));
        break;
    }
}

 
Best Answer chosen by LIM AI KHOON
CharuDuttCharuDutt
Hii LIM AI KHOON
Try Below Test Class
@isTest
public class DeleteContentDocumentTest {
@isTest
    public Static Void UnitTest(){
      
        Profile p = [SELECT Id FROM Profile WHERE Name='Office Profile'];
        UserRole ur = new UserRole(Name = 'CEO');/*Fill Role According To Your Org*/
        insert ur;
        User usr = new User(LastName = 'sample',
                            FirstName='sam',
                            Alias = 'simplSam',
                            Email = 'ssamsimple@asdf.com',
                            Username = 'ssamsimple@asdf.com',
                            ProfileId = p.id,
                            TimeZoneSidKey = 'GMT',
                            LanguageLocaleKey = 'en_US',
                            EmailEncodingKey = 'UTF-8',
                            LocaleSidKey = 'en_US', UserRoleId = ur.Id
                           );
        insert usr;
        system.runAs(usr){
            Centive__c C = new Centive__c ();
            C.Name= 'Test';
            /*Fill All Required Fields */
            Insert C;
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!