+ Start a Discussion
Anthony HuerterAnthony 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;
  } 
 
Pankaj_GanwaniPankaj_Ganwani
Hi,

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...
  } 
 
geeta garggeeta garg
Hi Anthony Huerter,

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
 
Anthony HuerterAnthony Huerter
When I make the update to the trigger to say "if (opp.accountId != null) I get this error on lead conversions 

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:
 
geeta garggeeta garg
Hi Anthony Huerter,

Did you write any code for lead conversion or u are using standard functionality?


Thanks,
Geeta Garg
Anthony HuerterAnthony Huerter
I didnt write them, but someone prior to me did.  We have quite a bit of code around auto conversions.

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.
Mahesh DMahesh D
Hi Anthony,

Please paste your trigger here using the above panel (Add a code sample < >) that way it will be easy to help you.

Regards,
Mahesh
Anthony HuerterAnthony Huerter
trigger OpportuntiyTerritoryID on Opportunity (before update) 
{
    //Id territortyId = [select id from Territory where name ='dfdfd'];

    for(opportunity opp : trigger.new)
     if (opp.accountId != null)
     
        if (opp.Transaction_Territory__c == 'South East')
    {Opp.Territory2Id = '0MI180000004DOG';// Try to get this ID from Query for using for loop(like territortyId)
    }
    else if(opp.Transaction_Territory__c == 'Mid Atlantic')
    {Opp.Territory2Id = '0MI180000004DOF';// Try to get this ID from Query for using for loop(like territortyId)
        }
       else if(opp.Transaction_Territory__c == 'East Central')
    {Opp.Territory2Id = '0MI180000004DO7';// Try to get this ID from Query for using for loop(like territortyId)
        }

       else if(opp.Transaction_Territory__c == 'West Central')
    {Opp.Territory2Id = '0MI180000004DO5';// Try to get this ID from Query for using for loop(like territortyId)
        }

       else if(opp.Transaction_Territory__c == 'Texas')
    {Opp.Territory2Id = '0MI180000004DO8';// Try to get this ID from Query for using for loop(like territortyId)
        }

       else if(opp.Transaction_Territory__c == 'Great Plains')
    {Opp.Territory2Id = '0MI180000004DOA';// Try to get this ID from Query for using for loop(like territortyId)
        }
       else if(opp.Transaction_Territory__c == 'North West')
    {Opp.Territory2Id = '0MI180000004DO9';// Try to get this ID from Query for using for loop(like territortyId)
        }
           else if(opp.Transaction_Territory__c == 'DACH')
    {Opp.Territory2Id = '';// Try to get this ID from Query for using for loop(like territortyId)
        }

       else if(opp.Transaction_Territory__c == 'Alliances')
    {Opp.Territory2Id = '';// Try to get this ID from Query for using for loop(like territortyId)
        }
        
       else if(opp.Transaction_Territory__c.contains ('New York'))
    {Opp.Territory2Id = '0MI180000004DOD';// Try to get this ID from Query for using for loop(like territortyId)
        }
       else if(opp.Transaction_Territory__c.contains ('NJ'))
    {Opp.Territory2Id = '0MI180000004DOE';// Try to get this ID from Query for using for loop(like territortyId)
        }
           else if(opp.Transaction_Territory__c.contains ('HI'))
    {Opp.Territory2Id = '0MI180000004DOB';// Try to get this ID from Query for using for loop(like territortyId)
        }
           else if(opp.Transaction_Territory__c.contains ('Great Lakes'))
    {Opp.Territory2Id = '0MI180000004DO6';// Try to get this ID from Query for using for loop(like territortyId)
        }
}

I appreciate the help with this.  This is my first trigger and apex test class so I may be really close or way wrong.
Mahesh DMahesh D
Hi Anthony,

Please try the below code:
 
trigger OpportuntiyTerritoryID on Opportunity (before update) 
{
    //Id territortyId = [select id from Territory where name ='dfdfd'];

    for(opportunity opp : trigger.new) {
		if (opp.accountId != null) {     
			if (opp.Transaction_Territory__c == 'South East')
			{Opp.Territory2Id = '0MI180000004DOG';// Try to get this ID from Query for using for loop(like territortyId)
			}
			else if(opp.Transaction_Territory__c == 'Mid Atlantic')
			{Opp.Territory2Id = '0MI180000004DOF';// Try to get this ID from Query for using for loop(like territortyId)
				}
			   else if(opp.Transaction_Territory__c == 'East Central')
			{Opp.Territory2Id = '0MI180000004DO7';// Try to get this ID from Query for using for loop(like territortyId)
				}

			   else if(opp.Transaction_Territory__c == 'West Central')
			{Opp.Territory2Id = '0MI180000004DO5';// Try to get this ID from Query for using for loop(like territortyId)
				}

			   else if(opp.Transaction_Territory__c == 'Texas')
			{Opp.Territory2Id = '0MI180000004DO8';// Try to get this ID from Query for using for loop(like territortyId)
				}

			   else if(opp.Transaction_Territory__c == 'Great Plains')
			{Opp.Territory2Id = '0MI180000004DOA';// Try to get this ID from Query for using for loop(like territortyId)
				}
			   else if(opp.Transaction_Territory__c == 'North West')
			{Opp.Territory2Id = '0MI180000004DO9';// Try to get this ID from Query for using for loop(like territortyId)
				}
				   else if(opp.Transaction_Territory__c == 'DACH')
			{Opp.Territory2Id = '';// Try to get this ID from Query for using for loop(like territortyId)
				}

			   else if(opp.Transaction_Territory__c == 'Alliances')
			{Opp.Territory2Id = '';// Try to get this ID from Query for using for loop(like territortyId)
				}
				
			   else if(opp.Transaction_Territory__c.contains ('New York'))
			{Opp.Territory2Id = '0MI180000004DOD';// Try to get this ID from Query for using for loop(like territortyId)
				}
			   else if(opp.Transaction_Territory__c.contains ('NJ'))
			{Opp.Territory2Id = '0MI180000004DOE';// Try to get this ID from Query for using for loop(like territortyId)
				}
				   else if(opp.Transaction_Territory__c.contains ('HI'))
			{Opp.Territory2Id = '0MI180000004DOB';// Try to get this ID from Query for using for loop(like territortyId)
				}
				   else if(opp.Transaction_Territory__c.contains ('Great Lakes'))
			{Opp.Territory2Id = '0MI180000004DO6';// Try to get this ID from Query for using for loop(like territortyId)
			}
		}
	}
}

If you get any error, please paste the full error message.

Regards,
Mahesh
Anthony HuerterAnthony Huerter
When trying to convert a lead.

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
Mahesh DMahesh D
To solve this issue:

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
Anthony HuerterAnthony Huerter
I dont see a Apex Class called 'Lead Convert'  I found one very similar '/* * class:  LeadConversionHelper

Line 104 for that Apex class I found
Merge convertedContact contact;