You need to sign in to do that
Don't have an account?

how to write test class for this trigger
trigger Dynamic_Lookup on OpportunityUserAssociation__c(before insert)
{
OpportunityUserAssociation__c myopportunity = trigger.new[0];
String Newuser;
String Flag='No';
Integer LRcount=100;
Integer LRtotcount=100;
Integer RMcount=0;
Integer loopi=0, CurrUserCnt=0, AllUserCnt=0;
AggregateResult[] val;
// changed if condition to send error message only if the opportunity is being assigned to lead recruiter
if(myopportunity.AssignedUser__c == null && myopportunity.AssignedUser__r.UserRole.Name == 'Lead Recruiter' )
{
trigger.new[0].AssignedUser__c.addError(' Select User');
}
if(myopportunity.AssignedUser__c!= null)
{
val = [ Select count(OpportunityUserAssociation__c.AssignedUser__c) cnt from OpportunityUserAssociation__c
where OpportunityUserAssociation__c.Status__c='Open'
and OpportunityUserAssociation__c.AssignedUser__c=:myopportunity.AssignedUser__c and OpportunityUserAssociation__c.AssignedUser__r.UserRole.Name!='Recruiter'];
for(AggregateResult ar1 : val)
{
LRcount = Integer.valueOf(ar1.get('cnt'));
CurrUserCnt = LRcount + 1;
if(Integer.valueOf(ar1.get('cnt'))>=3)
{
//LRcount = Integer.valueOf(ar1.get('cnt'));
LRcount = 100;
trigger.new[0].AssignedUser__c.addError('Selected LR have maximum Assignments');
//breakstart;
//breakend;
}
}
}
if(CurrUserCnt==3)
{
LIST<User> UserList = [Select u.Id from User u where
u.UserRole.Name = 'Lead Recruiter'
and u.UserRole.ParentRoleId != null
and u.ManagerId!=null
and u.ManagerId = :myopportunity.ManagerId__c and u.Id!=:myopportunity.AssignedUser__c];
for(Integer i = 0; i<UserList.size(); i++)
{
Newuser = UserList[i].Id;
//AggregateResult[] val = [Select count(Assign_To__c) cnt from Opportunity where Opportunity.Status__c='Open' and Opportunity.Assign_To__c=:Newuser ];
val = [Select count(OpportunityUserAssociation__c.AssignedUser__c) cnt from OpportunityUserAssociation__c where OpportunityUserAssociation__c.Status__c='Open' and OpportunityUserAssociation__c.AssignedUser__c=:Newuser ];
for(AggregateResult ar1 : val)
{
loopi=loopi+1;
System.debug('UUser : '+ i + ' : ' +Newuser );
System.debug('ccount : '+ i + ' : ' +ar1.get('cnt'));
if(Integer.valueOf(ar1.get('cnt'))==3)
{
AllUserCnt=1;
}
else
{
AllUserCnt=2;
break;
}
if(Integer.valueOf(ar1.get('cnt'))<=3)
{
LRcount = Integer.valueOf(ar1.get('cnt'));
LRcount =LRcount +1;
//System.debug('Count : '+ i + ' : ' +LRcount );
//trigger.new[0].Assign_To__c.addError('Selected LR have maximum Assignments');
//break;
}
}
if(AllUserCnt==2){break;}
if(loopi==0)
{LRcount=0;}
}
}// else part
if(string.valueOf(LRcount)!='')
{
//trigger.new[0].LR_Count__c=string.valueOf(LRcount);
trigger.new[0].LR_Count__c='NO';
//LIST<Opportunity> Opportunity = [update Opportunity set LR_Count__c = 'No'];
}
else
{
trigger.new[0].LR_Count__c='NOooo';
//LIST<Opportunity> Opportunity = [update Opportunity set LR_Count__c = 'No'];
}
//if(LRcount==100 )
if(AllUserCnt==1)
{
Flag='Yes';
}
for(OpportunityUserAssociation__c myopportunity1 : trigger.new)
{
myopportunity1.LR_Count__c = Flag;
//myopportunity1.LR_Count__c = string.valueOf(LRcount);
//myopportunity1.LR_Count__c = string.valueOf(AllUserCnt);
}
}
Hi
Try the steps this is just as reference,
@istest
private class Dynamic_Lookup_Test{
static testmethod void Dynamic_Lookup_Test(){
OpportunityUserAssociation__c opa = new OpportunityUserAssociation__c();
opa.AssignedUser__c='somethin insert if it is look up means insert';
opa.
op.
//like this insert the mandatory fields in AssignedUser__c Here insert according to if conditions
insert opa;
Opportunity opp = new Opportunity();
opp.......
opp.......
///like this insert the mandatory fields in Opportunity Here insert according to if conditions
insert opa;
insert opp;
}}
Regards,
Rajesh.