You need to sign in to do that
Don't have an account?
Ganu
Test Method for OpportunityContactRole
Hi,
I have written a Test Method for the below Trigger but am getting only 44% of test coverage. This trigger finds the Max of Close date from the Opportunities and calculates the sum of Opportunity Amount.
Please! Can anybody give a suggestion for this?
My code:
trigger DonationUpdateonContact on Opportunity bulk(after insert, after update)
{
if(Trigger.isInsert || Trigger.isupdate)
{
for (Opportunity opp :Trigger.new)
{
Id OppId;
OppId = opp.Id;
Date Contactdate;
Double totgift=0;
OpportunityContactRole[] ThisOppRole = [select ContactId,Opportunity.Amount,Opportunity.CloseDate From
OpportunityContactRole WHERE Opportunity.Id = :opp.Id];
if (ThisOppRole.size()>0)
{
OpportunityContactRole[] ThisOppRole1=[select Id,ContactId,Opportunity.Amount,Opportunity.CloseDate From
OpportunityContactRole WHERE ContactId= :ThisOppRole[0].ContactId order by Opportunity.CloseDate DESC];
if (ThisOppRole1.size()>0)
{
Contactdate = ThisOppRole1[0].Opportunity.CloseDate;
for(integer i=0;i<ThisOppRole1.size();i++)
{
totgift+=ThisOppRole1[i].Opportunity.Amount;
}
Contact CurrentContact = [select Id,Last_Donation_Date__c,Total_of_all_gifts_given__c from Contact
where Id=:ThisOppRole1[0].ContactId];
CurrentContact.Total_of_all_gifts_given__c=totgift;
CurrentContact.Last_Donation_Date__c=Contactdate;
update CurrentContact;
}
}
}
}
Test Method:
public class DonationUpdateoncontacttest
{
public static testMethod void DonationUpdateonContact()
{
Id OppId;
Date Contactdate;
Double totgift=0;
Id ContactId=null;
integer i=0;
Account acc=new Account(Name='NewAcc');
insert acc;
Contact cp= new Contact(LastName='ContactTest',AccountId=acc.Id,Contact_type__c='Donor',Donor_Status__c='Active',Last_Donation_Date__c=system.today(),Total_of_all_gifts_given__c=12,Boardmember_type__c='Board chair',Member_Type__c='Current',Staff_Status__c='Current staff');
insert cp;
Opportunity oppc=new Opportunity (Name='Newopp',StageName='Posted',closedate=system.today(),Amount=10);
insert oppc;
OpportunityContactRole oppr=new OpportunityContactRole(Role='Donor',OpportunityId=oppc.Id,ContactId=cp.Id,Isprimary=true);
insert oppr;
OpportunityContactRole[] ThisOppRole = [select ContactId,Opportunity.Amount,Opportunity.CloseDate From
OpportunityContactRole WHERE Opportunity.Id = :oppc.Id];
OpportunityContactRole[] ThisOppRole1 = [select Id,ContactId,Opportunity.Amount,Opportunity.CloseDate From
OpportunityContactRole WHERE ContactId= :ThisOppRole[0].ContactId order by Opportunity.CloseDate DESC];
for(i=0;i<ThisOppRole1.size();i++)
{
totgift+=ThisOppRole1[i].Opportunity.Amount;
}
Contactdate = ThisOppRole1[0].Opportunity.CloseDate;
Contact CurrentContact = [select Id,Last_Donation_Date__c,Total_of_all_gifts_given__c from Contact
where Id=:ThisOppRole1[0].ContactId];
CurrentContact.Total_of_all_gifts_given__c=totgift;
CurrentContact.Last_Donation_Date__c=Contactdate;
update CurrentContact;
}
}
{
if(Trigger.isInsert || Trigger.isupdate)
{
for (Opportunity opp :Trigger.new)
{
Id OppId;
OppId = opp.Id;
Date Contactdate;
Double totgift=0;
OpportunityContactRole[] ThisOppRole = [select ContactId,Opportunity.Amount,Opportunity.CloseDate From
OpportunityContactRole WHERE Opportunity.Id = :opp.Id];
if (ThisOppRole.size()>0)
{
OpportunityContactRole[] ThisOppRole1=[select Id,ContactId,Opportunity.Amount,Opportunity.CloseDate From
OpportunityContactRole WHERE ContactId= :ThisOppRole[0].ContactId order by Opportunity.CloseDate DESC];
if (ThisOppRole1.size()>0)
{
Contactdate = ThisOppRole1[0].Opportunity.CloseDate;
for(integer i=0;i<ThisOppRole1.size();i++)
{
totgift+=ThisOppRole1[i].Opportunity.Amount;
}
Contact CurrentContact = [select Id,Last_Donation_Date__c,Total_of_all_gifts_given__c from Contact
where Id=:ThisOppRole1[0].ContactId];
CurrentContact.Total_of_all_gifts_given__c=totgift;
CurrentContact.Last_Donation_Date__c=Contactdate;
update CurrentContact;
}
}
}
}
Test Method:
public class DonationUpdateoncontacttest
{
public static testMethod void DonationUpdateonContact()
{
Id OppId;
Date Contactdate;
Double totgift=0;
Id ContactId=null;
integer i=0;
Account acc=new Account(Name='NewAcc');
insert acc;
Contact cp= new Contact(LastName='ContactTest',AccountId=acc.Id,Contact_type__c='Donor',Donor_Status__c='Active',Last_Donation_Date__c=system.today(),Total_of_all_gifts_given__c=12,Boardmember_type__c='Board chair',Member_Type__c='Current',Staff_Status__c='Current staff');
insert cp;
Opportunity oppc=new Opportunity (Name='Newopp',StageName='Posted',closedate=system.today(),Amount=10);
insert oppc;
OpportunityContactRole oppr=new OpportunityContactRole(Role='Donor',OpportunityId=oppc.Id,ContactId=cp.Id,Isprimary=true);
insert oppr;
OpportunityContactRole[] ThisOppRole = [select ContactId,Opportunity.Amount,Opportunity.CloseDate From
OpportunityContactRole WHERE Opportunity.Id = :oppc.Id];
OpportunityContactRole[] ThisOppRole1 = [select Id,ContactId,Opportunity.Amount,Opportunity.CloseDate From
OpportunityContactRole WHERE ContactId= :ThisOppRole[0].ContactId order by Opportunity.CloseDate DESC];
for(i=0;i<ThisOppRole1.size();i++)
{
totgift+=ThisOppRole1[i].Opportunity.Amount;
}
Contactdate = ThisOppRole1[0].Opportunity.CloseDate;
Contact CurrentContact = [select Id,Last_Donation_Date__c,Total_of_all_gifts_given__c from Contact
where Id=:ThisOppRole1[0].ContactId];
CurrentContact.Total_of_all_gifts_given__c=totgift;
CurrentContact.Last_Donation_Date__c=Contactdate;
update CurrentContact;
}
}
Thanks,
Ganu
-- Steve Lane
Hi Steve Lane,
Thanks for your effort, I fixed the issue by updating the OpportunityContactRole.
And I get 100% of code coverage.
--Ganu