You need to sign in to do that
Don't have an account?
Amulya
Code coverage for if
Hi All,
I am not able to write the test class code to cover highlighted lines in below trigger code. Please help.
trigger AddPrimaryContRole on Opportunity (after insert, after update)
{
for (Opportunity o : Trigger.new)
{
Integer i=0, j=0;
if(Trigger.isUpdate && (o.Primary_Contact_Name__c != trigger.oldMap.get(o.Id).Primary_Contact_Name__c || o.Primary_Contact_Role__c !=trigger.oldMap.get(o.Id).Primary_Contact_Role__c))
{
List<OpportunityContactRole> ocrl = [select id, ContactId, Opportunityid, role, isPrimary from OpportunityContactRole where opportunityid =: o.Id];
system.debug('Queried OCR:'+ocrl);
if(ocrl.size() == 0)
{
system.debug('Entered IF');
OpportunityContactRole new_ocr = new OpportunityContactRole();
new_ocr.opportunityid = o.Id;
new_ocr.role = o.Primary_Contact_Role__c;
new_ocr.ContactId = o.Primary_Contact_Name__c;
new_ocr.isPrimary = true;
Insert(new_ocr);
system.debug('Inserted OCR:'+new_ocr);
}
else
{
for(OpportunityContactRole oc: ocrl)
{
if(oc.contactId == o.Primary_Contact_Name__c)
{
i++;
oc.isPrimary = true;
oc.role = o.Primary_Contact_Role__c;
Update oc;
}
}
if(i==0)
j++;
}
}
if(Trigger.isInsert || j>0)
{
OpportunityContactRole new_ocr = new OpportunityContactRole();
new_ocr.opportunityid = o.Id;
new_ocr.role = o.Primary_Contact_Role__c;
new_ocr.ContactId = o.Primary_Contact_Name__c;
new_ocr.isPrimary = true;
Insert(new_ocr);
}
}
}
I am not able to write the test class code to cover highlighted lines in below trigger code. Please help.
trigger AddPrimaryContRole on Opportunity (after insert, after update)
{
for (Opportunity o : Trigger.new)
{
Integer i=0, j=0;
if(Trigger.isUpdate && (o.Primary_Contact_Name__c != trigger.oldMap.get(o.Id).Primary_Contact_Name__c || o.Primary_Contact_Role__c !=trigger.oldMap.get(o.Id).Primary_Contact_Role__c))
{
List<OpportunityContactRole> ocrl = [select id, ContactId, Opportunityid, role, isPrimary from OpportunityContactRole where opportunityid =: o.Id];
system.debug('Queried OCR:'+ocrl);
if(ocrl.size() == 0)
{
system.debug('Entered IF');
OpportunityContactRole new_ocr = new OpportunityContactRole();
new_ocr.opportunityid = o.Id;
new_ocr.role = o.Primary_Contact_Role__c;
new_ocr.ContactId = o.Primary_Contact_Name__c;
new_ocr.isPrimary = true;
Insert(new_ocr);
system.debug('Inserted OCR:'+new_ocr);
}
else
{
for(OpportunityContactRole oc: ocrl)
{
if(oc.contactId == o.Primary_Contact_Name__c)
{
i++;
oc.isPrimary = true;
oc.role = o.Primary_Contact_Role__c;
Update oc;
}
}
if(i==0)
j++;
}
}
if(Trigger.isInsert || j>0)
{
OpportunityContactRole new_ocr = new OpportunityContactRole();
new_ocr.opportunityid = o.Id;
new_ocr.role = o.Primary_Contact_Role__c;
new_ocr.ContactId = o.Primary_Contact_Name__c;
new_ocr.isPrimary = true;
Insert(new_ocr);
}
}
}
All Answers
OpportunityContactRole new_ocr = new OpportunityContactRole();
new_ocr.opportunityid = o.Id;
new_ocr.role = o.Primary_Contact_Role__c;
new_ocr.ContactId = o.Primary_Contact_Name__c;
new_ocr.isPrimary = true;
Insert(new_ocr);