You need to sign in to do that
Don't have an account?
raman123
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 );
}
}
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 );
}
}
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, regards
Sampath