You need to sign in to do that
Don't have an account?
Guru@SfCloud
Test Code Coverage for Lead Conversion
Hi ,
I created trigger for lead conversion and i am doing the lead conversion in class and its working properly but i am facing the issue with the Test code coverage for classand my trigger had 100 % code coverage
Right now i am getting only 61% code coverage for my test class and please find the both trigger and class as well.
Trigger
------------:
trigger Leads on Lead (after update) {
if(Trigger.isAfter && Trigger.isUpdate){
Leads l = new Leads();
l.SetContactRoleDefaults(Trigger.new, Trigger.oldMap);
}
for this i had 100% code coverage
Class
======:
public class Leads {
// Sets default values on the Opportunity and Opportunity Contact Role record created during Conversion. Called on AFTER UPDATE
public void SetContactRoleDefaults(Lead[] leads, map<ID,Lead> old_leads)
{
set<ID> set_opptyIDs = new set<ID>();
// Get Opportunity IDs into a Set if the lead was just converted and an Opportunity was created
for (Lead l:leads){
if (l.IsConverted && !old_leads.get(l.id).IsConverted){
if (l.ConvertedOpportunityId != null){
set_opptyIDs.add(l.ConvertedOpportunityId);
}
}
}
// Update Opportunity Contact Roles
list<OpportunityContactRole> list_opptyContactRolesToUpdate = new list<OpportunityContactRole>();
for(OpportunityContactRole ocr:[select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId in :set_opptyIDs]) {
ocr.IsPrimary = true;
ocr.Role = 'Decision Maker'; // set to what you want defaulted
list_opptyContactRolesToUpdate.add(ocr);
}
System.debug('list_opptyContactRolesToUpdate size : '+list_opptyContactRolesToUpdate.size());
if (list_opptyContactRolesToUpdate.size() > 0) {
System.debug('list_opptyContactRolesToUpdate : '+list_opptyContactRolesToUpdate);
delete list_opptyContactRolesToUpdate;
}
}
}
Bold lines are not get covered
Test Class:
==========
@IsTest
private class Leads_Test {
static testMethod void SetContactRoleDefaults_Test() {
RecordType Rectype = [Select id from RecordType where name=:'Insurance Provider'];
Account Acc = new Account();
Acc.Name = 'Company';
Acc.RecordTypeId = Rectype.Id;
Insert Acc;
Lead l = new Lead();
l.lastname = 'Lastname';
l.firstname = 'FirstName';
l.company = 'Company';
//l.Ready_to_Convert__c = true;
insert l;
System.debug('--------l Value---------'+l);
User testUser1 = new User();
//Convert the Lead
test.startTest();
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(l.id);
lc.setDoNotCreateOpportunity(true);
lc.setOwnerId(testUser1.id);
lc.setConvertedStatus('Qualified');
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
Lead existingLead = [Select id, isConverted, ConvertedAccountId, ConvertedOpportunityId, createdbyid FROM Lead where id = :l.id];
System.assert(existingLead.isConverted);
System.assert(existingLead.ConvertedAccountId != null);
System.assert(existingLead.ConvertedOpportunityId != null);
test.stopTest();
}
}
Right now my code coverage is 61% can any help me to reach the 100%
if any one help would be greatly appriciated .
I created trigger for lead conversion and i am doing the lead conversion in class and its working properly but i am facing the issue with the Test code coverage for classand my trigger had 100 % code coverage
Right now i am getting only 61% code coverage for my test class and please find the both trigger and class as well.
Trigger
------------:
trigger Leads on Lead (after update) {
if(Trigger.isAfter && Trigger.isUpdate){
Leads l = new Leads();
l.SetContactRoleDefaults(Trigger.new, Trigger.oldMap);
}
for this i had 100% code coverage
Class
======:
public class Leads {
// Sets default values on the Opportunity and Opportunity Contact Role record created during Conversion. Called on AFTER UPDATE
public void SetContactRoleDefaults(Lead[] leads, map<ID,Lead> old_leads)
{
set<ID> set_opptyIDs = new set<ID>();
// Get Opportunity IDs into a Set if the lead was just converted and an Opportunity was created
for (Lead l:leads){
if (l.IsConverted && !old_leads.get(l.id).IsConverted){
if (l.ConvertedOpportunityId != null){
set_opptyIDs.add(l.ConvertedOpportunityId);
}
}
}
// Update Opportunity Contact Roles
list<OpportunityContactRole> list_opptyContactRolesToUpdate = new list<OpportunityContactRole>();
for(OpportunityContactRole ocr:[select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId in :set_opptyIDs]) {
ocr.IsPrimary = true;
ocr.Role = 'Decision Maker'; // set to what you want defaulted
list_opptyContactRolesToUpdate.add(ocr);
}
System.debug('list_opptyContactRolesToUpdate size : '+list_opptyContactRolesToUpdate.size());
if (list_opptyContactRolesToUpdate.size() > 0) {
System.debug('list_opptyContactRolesToUpdate : '+list_opptyContactRolesToUpdate);
delete list_opptyContactRolesToUpdate;
}
}
}
Bold lines are not get covered
Test Class:
==========
@IsTest
private class Leads_Test {
static testMethod void SetContactRoleDefaults_Test() {
RecordType Rectype = [Select id from RecordType where name=:'Insurance Provider'];
Account Acc = new Account();
Acc.Name = 'Company';
Acc.RecordTypeId = Rectype.Id;
Insert Acc;
Lead l = new Lead();
l.lastname = 'Lastname';
l.firstname = 'FirstName';
l.company = 'Company';
//l.Ready_to_Convert__c = true;
insert l;
System.debug('--------l Value---------'+l);
User testUser1 = new User();
//Convert the Lead
test.startTest();
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(l.id);
lc.setDoNotCreateOpportunity(true);
lc.setOwnerId(testUser1.id);
lc.setConvertedStatus('Qualified');
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
Lead existingLead = [Select id, isConverted, ConvertedAccountId, ConvertedOpportunityId, createdbyid FROM Lead where id = :l.id];
System.assert(existingLead.isConverted);
System.assert(existingLead.ConvertedAccountId != null);
System.assert(existingLead.ConvertedOpportunityId != null);
test.stopTest();
}
}
Right now my code coverage is 61% can any help me to reach the 100%
if any one help would be greatly appriciated .
You are not inserting any OpportunityContactRole , so insert this object according to your triggers requirement, and then check your code coverage.
Regards,
Abhi Tripathi
Salesforce Developer