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

Help with Test Class for Auto Creating a Contact Role on Opportunity
Hello
I am trying to Cover this trigger
trigger addContactRole on Opportunity (after Insert, after update) {
List<OpportunityContactRole> newContactRoleList=new List<OpportunityContactRole>();
List<OpportunityContactRole> oldContactRoleList=new List<OpportunityContactRole>();
Set<Id> OppId=new Set<Id>();
Set<Id> ContactId=new Set<Id>();
for(Opportunity oppObj: Trigger.new)
{
//Insert condition
if(Trigger.isInsert)
{
if(oppObj.Contact__c!=null )
{
//Creating new contact role
newContactRoleList.add(new OpportunityContactRole (ContactId=oppObj.Contact__c,OpportunityId=oppObj.Id, Role='Decision Maker', IsPrimary = true));
}
}
else
{
if(oppObj.Contact__c==null && Trigger.oldMap.get(oppObj.Id).Contact__c!=null)
{
//Getting the contact and oppty Id from old values and adding this in set
Opportunity OldoppObj=Trigger.oldMap.get(oppObj.Id);
OppId.add(OldoppObj.id);
ContactId.add(OldoppObj.Contact__c);
}
else if(oppObj.Contact__c!=null && Trigger.oldMap.get(oppObj.Id).Contact__c==null)
{
//Creating new contact role
newContactRoleList.add(new OpportunityContactRole (ContactId=oppObj.Contact__c, OpportunityId=oppObj.Id, Role='Decision Maker', IsPrimary = true));
}
}
}
try
{
//inserting new Contacts
if(newContactRoleList.size()>0) insert newContactRoleList;
//Selecting old contact roles
if (OppId.size()>0) oldContactRoleList=[Select Id from OpportunityContactRole where ContactId in : ContactId and OpportunityId in : OppId];
//Deleting old contact roles
if (oldContactRoleList.size()>0) delete oldContactRoleList;
}
catch(Exception e)
{
System.debug(e);
//trigger.new[0].addError('Technical error occurred. Please contact to your system administrator or try after some time.');
}
}
but I cannot get my code coverage over 73 percent.
here is my test class to cover this code so far. Can anyone help me with this?
@isTest(SeeAllData=true)
private class TestaddContactRole {
static testmethod void TestaddContactRole() {
Test.startTest();
Business_Account__c b= new Business_Account__c(Name='Btest');
b.Customer_Type__c='Industry';
insert b;
Account a= new Account(Name='Test');
a.Business_Account__c=b.Id;
a.Lab_Purpose__c='Manufacturing';
a.Address__c='test';
a.City__c='test';
a.Country__c='United States';
a.Region__c='AMER';
a.Bill_To__c=true;
insert a;
// Create contacts
Contact ct1 = new Contact(AccountId=a.Id);
Contact ct2 = new Contact(AccountId=a.Id);
ct1.FirstName = 'Larrys';
ct1.LastName = 'Page';
ct2.FirstName = 'Larrys';
ct2.LastName = 'Page';
Insert ct1;
Insert ct2;
// Create Opportunity
Opportunity op1 = new Opportunity(AccountId=a.Id,Contact__c = ct1.Id,Bill_To_Lab__c=a.Id);
op1.Region__c='AMER';
op1.Buy_Gatan__c='100% - Wants Gatan, No Doubt';
op1.Buy__c='100% - Funding is secured';
op1.name = 'Test';
op1.CloseDate = Date.today()+2;
op1.StageName = 'EVALUATION';
op1.Sales_Channel__c='Distributor';
Database.SaveResult sr1 = Database.insert(op1, true);
System.assert(sr1.isSuccess());
system.debug('Inserted new opportunity');
// Update Opportunity with new contact
op1.Contact__c = ct2.Id;
//update op1;
Database.SaveResult sr2 = Database.update(op1, true);
System.assert(sr2.isSuccess());
system.debug('Opportunity updated');
Test.stopTest();
System.assert(sr2.isSuccess());
}
}