You need to sign in to do that
Don't have an account?
dhruv arora
Please help me in writting test class for this trigger
trigger Primaryopp on Opportunity_Product__c (after insert,after update)
{
set<id> Oppid = new set<id>();
for(Opportunity_Product__c Oppproduct : trigger.new)
{
Oppid.add(Oppproduct.abcd__lookup__c);
}
list<opportunity> Opplst = [SELECT Name,id,(select abcd__Primary__c from Opportunity_Products__r where abcd__Primary__c=True) FROM Opportunity WHERE id in:Oppid];
system.debug('##lst'+Opplst[0].Opportunity_Products__r.size());
for(opportunity opp: Opplst)
{
if(opp.Opportunity_Products__r.size()>1 )
{
trigger.new[0].addError('you can not check multiple primary opportunity');
}
}
}
{
set<id> Oppid = new set<id>();
for(Opportunity_Product__c Oppproduct : trigger.new)
{
Oppid.add(Oppproduct.abcd__lookup__c);
}
list<opportunity> Opplst = [SELECT Name,id,(select abcd__Primary__c from Opportunity_Products__r where abcd__Primary__c=True) FROM Opportunity WHERE id in:Oppid];
system.debug('##lst'+Opplst[0].Opportunity_Products__r.size());
for(opportunity opp: Opplst)
{
if(opp.Opportunity_Products__r.size()>1 )
{
trigger.new[0].addError('you can not check multiple primary opportunity');
}
}
}
You write a test class for this the same way that you would any other:
- Set up some data for the for Opportunity_Product__c object
- Verify the behaviour with asserts.
The fact that the class is used as a controller is fairly immaterial.
Please try below test class as sample one
Let us know if this will help you