You need to sign in to do that
Don't have an account?
Anthony Huerter
Help on code coverage for a trigger
I have built a trigger and was at 77% code coveragae until I had to make a modifcation and add in that If statement on line 5. Now my code coverage is at 7%. What am I doing wrong?
Tigger – I continue this same core code from line 3 to 7 – 11 times just with a different Transaction Territory
trigger OpportuntiyTerritoryID on Opportunity (before update)
{
//Id territortyId = [select id from Territory where name ='dfdfd'];
for(opportunity opp : trigger.new)
if (opp.account != null)
if (opp.Transaction_Territory__c == 'South East')
{Opp.Territory2Id = '0MIE0000000KyyT';// Try to get this ID from Query for using for loop(like territortyId)
My Test Class – this repeats 7 times
public class OpportuntiyTerritoryIDTest
{
static testMethod void test ()
{
Account acc = new Account(Name = 'Test Account');
insert acc;
Opportunity opp = new Opportunity(Name='TEST opp', StageName='Closed Won', Probability = 100, CloseDate=system.today());
opp.Type ='New Sale';
opp.accountid = acc.id;
opp.Transaction_Territory__c = 'Great Lakes';
opp.activationcode__c = '00001234561';
insert opp;
opp.Transaction_Territory__c = 'HI';
opp.Source_Type__c = 'Carbonite';
update opp;
}
Tigger – I continue this same core code from line 3 to 7 – 11 times just with a different Transaction Territory
trigger OpportuntiyTerritoryID on Opportunity (before update)
{
//Id territortyId = [select id from Territory where name ='dfdfd'];
for(opportunity opp : trigger.new)
if (opp.account != null)
if (opp.Transaction_Territory__c == 'South East')
{Opp.Territory2Id = '0MIE0000000KyyT';// Try to get this ID from Query for using for loop(like territortyId)
My Test Class – this repeats 7 times
public class OpportuntiyTerritoryIDTest
{
static testMethod void test ()
{
Account acc = new Account(Name = 'Test Account');
insert acc;
Opportunity opp = new Opportunity(Name='TEST opp', StageName='Closed Won', Probability = 100, CloseDate=system.today());
opp.Type ='New Sale';
opp.accountid = acc.id;
opp.Transaction_Territory__c = 'Great Lakes';
opp.activationcode__c = '00001234561';
insert opp;
opp.Transaction_Territory__c = 'HI';
opp.Source_Type__c = 'Carbonite';
update opp;
}
In this case, you will have to update your opportunity in test class with each of the Transaction_Territory__c values to cover all if-else blocks. Like:
public class OpportuntiyTerritoryIDTest
{
static testMethod void test ()
{
Account acc = new Account(Name = 'Test Account');
insert acc;
Opportunity opp = new Opportunity(Name='TEST opp', StageName='Closed Won', Probability = 100, CloseDate=system.today());
opp.Type ='New Sale';
opp.accountid = acc.id;
opp.Transaction_Territory__c = 'Great Lakes';
opp.activationcode__c = '00001234561';
insert opp;
opp.Transaction_Territory__c = 'HI';
opp.Source_Type__c = 'Carbonite';
update opp;
opp.Transaction_Territory__c = 'South East'; //This will cover the if condition where will be executed when Transaction_Territory__c is South East
opp.Source_Type__c = 'Carbonite';
update opp;
opp.Transaction_Territory__c = 'West';//This will cover the if condition where will be executed when Transaction_Territory__c is West
opp.Source_Type__c = 'Carbonite';
update opp;
-------
--------
so on...
}
you should use "if (opp.accountId != null)" instead if if (opp.account != null).
you can use this test class :-
public class OpportuntiyTerritoryIDTest
{
static testMethod void test ()
{
Account acc = new Account(Name = 'Test Account');
insert acc;
Opportunity opp = new Opportunity(Name='TEST opp', StageName='Closed Won', Probability = 100, CloseDate=system.today());
opp.Type ='New Sale';
opp.accountid = acc.id;
opp.Transaction_Territory__c = 'Great Lakes';
opp.activationcode__c = '00001234561';
insert opp;
opp.Transaction_Territory__c = 'South East';
opp.Source_Type__c = 'Carbonite';
update opp;
}
}
Thanks,
Geeta Garg
Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportuntiyTerritoryID: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.OpportuntiyTerritoryID:
Did you write any code for lead conversion or u are using standard functionality?
Thanks,
Geeta Garg
Is there a way I can avoid the lead conversion process on my opportuntiy trigger and only fire when the opporutntiy is edited? Or only fire when a new opportuntiy is created not through the lead conversion process? That would be the most ideal for me.
Please paste your trigger here using the above panel (Add a code sample < >) that way it will be easy to help you.
Regards,
Mahesh
I appreciate the help with this. This is my first trigger and apex test class so I may be really close or way wrong.
Please try the below code:
If you get any error, please paste the full error message.
Regards,
Mahesh
Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportuntiyTerritoryID: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.OpportuntiyTerritoryID: line 39, column 1: [] Class.leadconvert.BulkLeadConvert.handleOpportunityInserts: line 730, column 1 Class.leadconvert.BulkLeadConvert.convertLead: line 104, column 1
Class.leadconvert.BulkLeadConvert.convertLead: line 104, column 1
Please paste the code of leadconvert class. mainly pointing to 104 line number. There may be some null check it missing.
Regards,
Mahesh
Line 104 for that Apex class I found