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
raman123raman123 

my test class is is covering 66% and also failing can you tell me whats the error

my code where it ppopulate the hourly rate field of team object from user hourly rate field if team hourly rate is null
trigger populate1 on Team__c (before insert , before update){ 
   Set<Id> idOwnerSet = new Set<Id>();
    List<Team__c> projectList = new List<Team__c>(); 
    for(Team__c obj : trigger.new)
    {
        if(obj.hourly_rate__c == null) {
            
            idOwnerSet.add(obj.Id);
            projectList.add(obj);
        }
       
    }    
    
    if(projectList.isEmpty()) return;
    
    Map<Id, User> userMap = new Map<Id, User>([SELECT Id, hourly_rate__c FROM User WHERE Id = :idOwnerSet]);
    
    if(userMap.isEmpty()) return;
    
    for(Team__c proj : projectList)
    {
        proj.hourly_rate__c = null;
        if(userMap.get(proj.Id).City != null)
        {
            proj.hourly_rate__c = userMap.get(proj.Id).hourly_rate__c;
        }
    }
      
}
my test class
@istest
public class testpopulate1 {
   @istest static  void checkrate(){
    team__c  proj= new team__c();
       proj.hourly_rate__c = null;
       insert proj;
      Profile pf= [Select Id from profile where Name='System Administrator'];

       User u = new User(
     LastName = 'last',
     Email = 'puser000@amamama.com',
     Username = 'puser000@amamama.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'alias',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US',
     city = 'Usa',
     hourly_rate__c = 30,
     ProfileId = pf.Id);
      insert u;
      
     system.assert(proj.hourly_rate__c == 30 );
      
   }


}
Raj VakatiRaj Vakati
Use this
 
@istest
public class testpopulate1 {
   @istest static  void checkrate(){
    
      Profile pf= [Select Id from profile where Name='System Administrator'];

       User u = new User(
     LastName = 'last',
     Email = 'puser000@amamama.com',
     Username = 'puser000@amamama.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'alias',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US',
     city = 'Usa',
     hourly_rate__c = 30,
     ProfileId = pf.Id);
      insert u;
	  System.runAs(u){
	  
	  team__c  proj= new team__c();
       proj.hourly_rate__c = null;
       insert proj;
      
     system.assert(proj.hourly_rate__c == 30 );
	 }
      
   }


}

 
Sampath SuranjiSampath Suranji
Hi,

I think there is an issue in your trigger code. "idOwnerSet" should be a set of UserIds. but here you have insert Team__c Ids to it. Please try something like below,
trigger populate1 on Team__c (before insert , before update){ 
   Set<Id> idOwnerSet = new Set<Id>();
    List<Team__c> projectList = new List<Team__c>(); 
    for(Team__c obj : trigger.new)
    {
        if(obj.hourly_rate__c == null) {
            
            idOwnerSet.add(obj.ownerId);
            projectList.add(obj);
        }
       
    }    
    
    if(projectList.isEmpty()) return;
    system.debug('idOwnerSet '+idOwnerSet);
    Map<Id, User> userMap = new Map<Id, User>([SELECT Id, hourly_rate__c,City FROM User WHERE Id in :idOwnerSet]);
    
    if(userMap.isEmpty()) return;
    
    for(Team__c proj : projectList)
    {
        proj.hourly_rate__c = null;
        if(userMap.get(proj.OwnerId).City != null)
        {
            proj.hourly_rate__c = userMap.get(proj.OwnerId).hourly_rate__c;
        }
    }
      
}
@istest
public class testpopulate1 {
    @istest static  void checkrate(){
        team__c  proj= new team__c();
        proj.hourly_rate__c = null;
        
        Profile pf= [Select Id from profile where Name='System Administrator'];
        
        User u = new User(
            LastName = 'last',
            Email = 'puser000@amamama.com',
            Username = 'puser000@amamama.com' + System.currentTimeMillis(),
            CompanyName = 'TEST',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US',
            city = 'Usa',
            hourly_rate__c = 30,
            ProfileId = pf.Id);
        insert u;
        system.runAs(u) {
            insert proj;
            Team__c objTeam= [select id,hourly_rate__c from team__C limit 1];
            system.assert(objTeam.hourly_rate__c == 30 );
        }
        
        
    }
    
    
}
regards
Sampath