function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
levi6dblevi6db 

Having Trouble getting over 65% coverage

Hi all,

 

I created a custom object called Lead Contact.  It is located on the Lead Object.  When we have multiple Leads from the same company, we add the extra Leads to the Lead Contact object.  Once the Parent Lead is converted, all Lead Contacts need to be converted to Contacts and assigned to the Parent Account.

 

We also have a custom object called Locations that will be moved over upon conversion of the Lead.

 

The trigger works great in the Sandbox, but I cannot get over 65% coverage in production.  Any suggestions?

 

Trigger:

 

trigger LeadContactConvertAcc on Account (after insert, after update) {

    Account a = [select Id, Lead_Convert_Id__c from Account where Id = :Trigger.new[0].Id limit 1];

    if(a.Lead_Convert_ID__c != NULL) {
      
    Lead l = [select Id from Lead where Id = :a.Lead_Convert_Id__c Limit 1];
        
            for (Lead_Contact__c lc : [Select id, Converted_Account_ID_del__c, First_Name__c,
                   Last_Name__c,Title__c,Email__c,Secondary_Email__c,Phone__c,Fax__c,
                   Mobile__c,Mailing_Street__c,Mailing_City__c,Mailing_State__c,
                   Mailing_Zip__c,Mailing_Country__c 
                   FROM Lead_Contact__c 
                   WHERE Parent_Lead_ID_del__c = :a.Lead_Convert_Id__c]) 
                   
                   {
                           
                   Contact newContact = new Contact (
                     AccountId = a.Id,
                     FirstName = lc.First_Name__c,
                     LastName = lc.Last_Name__c,
                     Title = lc.Title__c,
                     Email = lc.Email__c,
                     Secondary_Email__c = lc.Secondary_Email__c,
                     Phone = lc.Phone__c,
                     Fax = lc.Fax__c,
                     MobilePhone = lc.Mobile__c,
                     MailingStreet = lc.Mailing_Street__c,
                     MailingCity = lc.Mailing_City__c,
                     MailingState = lc.Mailing_State__c,
                     MailingPostalCode = lc.Mailing_Zip__c,
                     MailingCountry = lc.Mailing_Country__c);
                     
                   insert newContact;  
                   
                   lc.Parent_Name_del__c = null;
                   
                   update lc;
     
                   }   
                   
            for (Location__c ln : [select Id, Lead__c, Account__c 
                from Location__c 
                WHERE Lead__c = :a.Lead_Convert_Id__c]) {
                
                ln.Account__c = a.Id;
                
                update ln;
                
     }        
   }
  }

  Class:

 

 

public class LeadContactConvert {

static testMethod void LeadContactConvert() {

Id a1Id;
Id l1Id;
Id lnId;
Id lcId;
Id c1Id;
Id c2Id;

    // CREATE LEAD
    Lead l1 = new Lead();
    
        l1.Lastname='leaddata';
        l1.Company='nav';
        l1.Leadsource='Calling Campaign';
        l1.IsConverted = false;
        l1.IsUnreadByOwner = true;
        l1.Status = 'qualified';
        
        Database.insert(l1);
        l1Id = l1.id;
       

    //CREATE LEAD CONTACT
    Lead_Contact__c lc = new Lead_Contact__c ();
      
        lc.First_Name__c = 'test';
        lc.Last_Name__c = 'test';
        lc.Title__c = 'test';
        lc.Email__c = 'test@test.com';
        lc.secondary_Email__c = 'test2@aol.com';
        lc.Phone__c = '1111111111';
        lc.fax__c = '2222222222';
        lc.Mobile__c = '3333333333';
        lc.Mailing_Street__c = '123 Main St';
        lc.Mailing_City__c = 'Phoenix';
        lc.Mailing_State__c = 'AZ';
        lc.Mailing_Zip__c = '85083';
        lc.Mailing_Country__c = 'USA';
        lc.Parent_Name_del__c = l1.Id;
       
       
       Database.insert(lc);         
       lcId = lc.id;
   
    //CREATE LOCATION
    Location__c  ln = New Location__c ();
      
      ln.Street__c = '123 test';
      ln.City__c = 'Test';
      ln.State__c = 'az';
      ln.zip__c = '11111';
      ln.Entrances_Required__c = 'test';
      ln.Location__c = 1;
      ln.Proposed_Demarc__c = 'test';
      ln.Account__c = NULL;
      ln.Lead__c = l1.Id;
          
      Database.insert(ln);
      lnId = ln.id;
       
    //CREATE ACCOUNT   
    Account a1 = new Account();
        a1.name = 'AName';
        a1.type = 'Prospect';
        a1.Lead_Convert_ID__c = l1.Id;
        
        Database.insert(a1);
        a1Id = a1.Id;
    
     //CREATE CONTACT 1
     Contact c1 = new Contact ();
      
       c1.LastName = l1.Lastname;
       c1.Email = 'test@aol.com';
       c1.Phone = '9999999999';
       c1.AccountId = a1.id;
       
       Database.insert(c1);            
       c1Id = c1.id; 
    
    
    //CREATE CONTACT 2
    Contact c2 = new Contact ();
      
        c2.FirstName = lc.First_Name__c;
        c2.LastName = lc.Last_Name__c;
        c2.Title = lc.Title__c;
        c2.Email = lc.Email__c;
        c2.Secondary_Email__c = lc.Secondary_Email__c;
        c2.Phone = lc.Phone__c;
        c2.Fax = lc.Fax__c;
        c2.MobilePhone = lc.Mobile__c;
        c2.MailingStreet = lc.Mailing_Street__c;
        c2.MailingCity = lc.Mailing_City__c;
        c2.MailingState = lc.Mailing_State__c;
        c2.MailingPostalCode = lc.Mailing_Zip__c;
        c2.MailingCountry = lc.Mailing_Country__c;
        c2.AccountId = lc.Converted_Account_ID_del__c;
       
       Database.insert(c2);            
       c2Id = c2.id;     
     
               
// Account a = [select Id, Lead_Convert_Id__c from Account where Lead_Convert_Id__c = :l1.Id limit 1];
      
//      Lead_Contact__c lc1 = [Select id, Converted_Account_ID_del__c, First_Name__c,
//                   Last_Name__c,Title__c,Email__c,Secondary_Email__c,Phone__c,Fax__c,
//                   Mobile__c,Mailing_Street__c,Mailing_City__c,Mailing_State__c,
//                   Mailing_Zip__c,Mailing_Country__c 
//                   FROM Lead_Contact__c 
//                   WHERE Parent_Name_del__c = :a.Lead_Convert_Id__c]; 
      
      Contact newContact = new Contact (
      AccountId = a1.Id,
      FirstName = lc.First_Name__c,
      LastName = lc.Last_Name__c,
      Title = lc.Title__c,
      Email = lc.Email__c,
      Phone = lc.Phone__c,
      Fax = lc.Fax__c,
      MobilePhone = lc.Mobile__c,
      MailingStreet = lc.Mailing_Street__c,
      MailingCity = lc.Mailing_City__c,
      MailingState = lc.Mailing_State__c,
      MailingPostalCode = lc.Mailing_Zip__c,
      MailingCountry = lc.Mailing_Country__c);
       
      insert newContact; 
        
      lc.Parent_Name_del__c = null;
       
        update lc; 
        
      ln.Account__c = a1.Id;
      
        update ln;  
        
     }
   
}

 

 

Thank you in advance for any assistance.

 

Levi

 

Best Answer chosen by Admin (Salesforce Developers) 
Osiris77706Osiris77706

 WHERE Parent_Lead_ID_del__c = :a.Lead_Convert_Id__c])

^ from what i can see this particular part of your soql statement is the problem, since the code that uses that particular statement is not being reached, i would say its safe to asssume that  the soql statement is returning nothing. You should look back over the code and make sure that it is exactly what you wanna say there.

 

Ask yourself:

 

Should this statement actually return something ?

 

Is a.Lead_Convert_Id__c  a null variable at execution ?

 

Is a.Lead_Convert_Id__c definately the right element to use for this situation ?

 

I'm sorry i cant be of more help. I cannot really look into your variables and your databse to ensure that the statement is the right one for what you are trying to do, but i am sure if you go through that statement enough and test all of the outcomes you will figure out why it's not returning anything.

 

 

******

EDIT

******

 

-Since your program works correctly as you say, id make sure that in your test class that you have appropriately assigned the variables so that the soql statement in question will return something, so that your test program will go through that part of the code, getting you to probably 100 % coverage.

 

Mind you it doesnt mean your code is broke, just that the test class you wrote is not testing enough of your code.

 

*******

All Answers

kwukwu

So you are trying to deploy this into production but it's not letting you because your code coverage is 65%?

levi6dblevi6db

Yes.

kwukwu

It looks to me like the test class should run through every line of code in your trigger. Try running your test method in anonymous apex and put some print statements in each control block and make sure the print statements are reached.

Osiris77706Osiris77706

When you run the test, it takes you to a page with feedback that looks like this:

 

APEX TEST RESULT

....

...

 

...

..

..

 

Go to the area that says "CODE COVERAGE". click on the percentage of coverage(in your case click on the 65%). it will bring up a window that has your code that was tested, the covered parts are in blue. the parts in red were not covered by your test. you need to add test code for some of those parts (preferibly all to soundly test your trigger, though you can successfully create a trigger with say 78 % coverage that works just fine.) to bring it up to the pass percentage. if you can't figure out how to test those parts in red, you can post them here and we can try to help. it would be alot easier for you to post those parts in red here, than for us to try to figure out which parts are tested by your code with only our eyes. =p

 

 

levi6dblevi6db

Here is the CODE Coverage:

 

 

LeadContactConvertAcc (Code Covered: 63%)

 line	  executions	 source
 1	 	  trigger LeadContactConvertAcc on Account (after insert, after update) {
 2	 	  
 3	 2	   Account a = [select Id, Lead_Convert_Id__c from Account where Id = :Trigger.new[0].Id limit 1];
 4	 	  
 5	 4	   if(a.Lead_Convert_ID__c != NULL) {
 6	 	  
 7	 2	   Lead l = [select Id from Lead where Id = :a.Lead_Convert_Id__c Limit 1];
 8	 	  
 9	 2	   for (Lead_Contact__c lc : [Select id, Converted_Account_ID_del__c, First_Name__c,
 10	 	   Last_Name__c,Title__c,Email__c,Secondary_Email__c,Phone__c,Fax__c,
 11	 	   Mobile__c,Mailing_Street__c,Mailing_City__c,Mailing_State__c,
 12	 	   Mailing_Zip__c,Mailing_Country__c
 13	 	   FROM Lead_Contact__c
 14	 	   WHERE Parent_Lead_ID_del__c = :a.Lead_Convert_Id__c])
 15	 	  
 16	 	   {
 17	 	  
 18	 0	   Contact newContact = new Contact (
 19	 	   AccountId = a.Id,
 20	 	   FirstName = lc.First_Name__c,
 21	 	   LastName = lc.Last_Name__c,
 22	 	   Title = lc.Title__c,
 23	 	   Email = lc.Email__c,
 24	 	   Secondary_Email__c = lc.Secondary_Email__c,
 25	 	   Phone = lc.Phone__c,
 26	 	   Fax = lc.Fax__c,
 27	 	   MobilePhone = lc.Mobile__c,
 28	 	   MailingStreet = lc.Mailing_Street__c,
 29	 	   MailingCity = lc.Mailing_City__c,
 30	 	   MailingState = lc.Mailing_State__c,
 31	 	   MailingPostalCode = lc.Mailing_Zip__c,
 32	 	   MailingCountry = lc.Mailing_Country__c);
 33	 	  
 34	 0	   insert newContact;
 35	 	  
 36	 0	   lc.Parent_Name_del__c = null;
 37	 	  
 38	 0	   update lc;
 39	 	  
 40	 	   }
 41	 	  
 42	 2	   for (Location__c ln : [select Id, Lead__c, Account__c
 43	 	   from Location__c
 44	 	   WHERE Lead__c = :a.Lead_Convert_Id__c]) {
 45	 	  
 46	 2	   ln.Account__c = a.Id;
 47	 	  
 48	 2	   update ln;
 49	 	  
 50	 	   }
 51	 	  
 52	 	  
 53	 	   }
 54	 	   }

 Not sure how to fix the Contact section.

 

Thanks for your help!!

 

Osiris77706Osiris77706

What is the purpose of the brackets on lines 16 and 50 ?

Osiris77706Osiris77706

Nvm i see the ' for 'now, gimme another second.

Osiris77706Osiris77706

 WHERE Parent_Lead_ID_del__c = :a.Lead_Convert_Id__c])

^ from what i can see this particular part of your soql statement is the problem, since the code that uses that particular statement is not being reached, i would say its safe to asssume that  the soql statement is returning nothing. You should look back over the code and make sure that it is exactly what you wanna say there.

 

Ask yourself:

 

Should this statement actually return something ?

 

Is a.Lead_Convert_Id__c  a null variable at execution ?

 

Is a.Lead_Convert_Id__c definately the right element to use for this situation ?

 

I'm sorry i cant be of more help. I cannot really look into your variables and your databse to ensure that the statement is the right one for what you are trying to do, but i am sure if you go through that statement enough and test all of the outcomes you will figure out why it's not returning anything.

 

 

******

EDIT

******

 

-Since your program works correctly as you say, id make sure that in your test class that you have appropriately assigned the variables so that the soql statement in question will return something, so that your test program will go through that part of the code, getting you to probably 100 % coverage.

 

Mind you it doesnt mean your code is broke, just that the test class you wrote is not testing enough of your code.

 

*******

This was selected as the best answer
Force.comForce.com

Well, your SOQL query might be  returning null and the code inside for loop doesnt gets executed, thats why your code coverage is low.

I think you need to check your where clause of the query.

 

Note: Trigger should be bulk safe which means it should handle large chunk of data at a time when importing through API else it would throw exception.

 

your code contains both SOQL queries and DML statements inside the loop, so could be an issue later on.

 

 

NaishadhNaishadh

Hi,

 

you forgot to set value for Parent_Lead_Id_del__c in Lead_Contact__c object. Set the value first and then try.

 

 

levi6dblevi6db

I bulkified the trigger and reworked it a bit based on all of your suggestions.  I works great!  Thanks for all of your help!

 

Trigger:

 

trigger LeadContactConvertAcc on Account (after insert, after update) {


List<Location__c> loclist = new List<Location__c>();
List<Contact> contlist = new List<Contact>();


    Account a = [select Id, OwnerId, Lead_Convert_Id__c from Account where Id = :Trigger.new[0].Id
                LIMIT 1];

    if(a.Lead_Convert_ID__c != NULL) {
      
    Lead l = [select Id, ConvertedAccountId from Lead where Id = :a.Lead_Convert_Id__c Limit 1];
        
            for (Lead_Contact__c lc : [Select id, Converted_Account_ID_del__c, Parent_Lead_ID_del__c,
                   First_Name__c, Last_Name__c,Title__c,Email__c,Secondary_Email__c,Phone__c,Fax__c,
                   Mobile__c,Mailing_Street__c,Mailing_City__c,Mailing_State__c,
                   Mailing_Zip__c,Mailing_Country__c, Parent_Name_del__c
                   FROM Lead_Contact__c 
                   WHERE Parent_Name_del__c = :a.Lead_Convert_Id__c]) 
                   
                   {
                   
                           
                   Contact newContact = new Contact (
                 //    AccountId = lc.Converted_Account_ID_del__c,
                     AccountId = a.Id, 
                     FirstName = lc.First_Name__c,
                     LastName = lc.Last_Name__c,
                     Title = lc.Title__c,
                     Email = lc.Email__c,
                     Secondary_Email__c = lc.Secondary_Email__c,
                     Phone = lc.Phone__c,
                     Fax = lc.Fax__c,
                     OwnerID = a.OwnerID,
                     MobilePhone = lc.Mobile__c,
                     MailingStreet = lc.Mailing_Street__c,
                     MailingCity = lc.Mailing_City__c,
                     MailingState = lc.Mailing_State__c,
                     MailingPostalCode = lc.Mailing_Zip__c,
                     MailingCountry = lc.Mailing_Country__c);
                     
                 //  insert newContact;  
                                      
                   contlist.add( newContact);
          
                   }
                
            for (Location__c ln : [select Id, Lead__c, Account__c 
                from Location__c 
                WHERE Lead__c = :a.Lead_Convert_Id__c]) {
                
                ln.Account__c = a.Id;
                
                loclist.add( ln);
                
                }
             
   insert contlist;
   update loclist;
   
   List<Lead_Contact__c> lcrlist = new List <Lead_Contact__c>();
   
   for (Lead_Contact__c lcr : [Select id, Parent_Name_del__c
                   FROM Lead_Contact__c 
                   WHERE Parent_Name_del__c = :a.Lead_Convert_Id__c]) 
   {
                lcr.Parent_Name_del__c = null;
                lcrlist.add(lcr);
    }            
    update lcrlist;
    
    a.Lead_Convert_Id__c = NULL; 
    update a;         
  }}