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
Pallavic14Pallavic14 

Help with test code for after insert on Lead object

Hi ,

I created a trigger on Lead object and some custom lookup fields to update after insert.
It works fine. I added test code and the code coverage is not covering all code.
Really appreciate of some one can help with what I missed. Here is my trigger and class.
FYI, my test code also includes convert lead whcih is extra.

***Trigger***
trigger UpdateLeadDistifields on Lead (after insert) {
    Set<ID> LeadId = new Set<Id>();
    for (Lead cLead : Trigger.New){
        
        Lead l = [select Id,OwnerId,PartnerAccountId,RecordTypeId from Lead where  Id = :cLead.Id];
        RecordType rt = [select Name FROM RecordType where Id = :l.RecordTypeId];
        
        
        if (rt.name == 'DealRegistration'){
            User u = [select Id ,AccountId from User where Id = :l.OwnerId and AccountId  != '' and ContactId != '' and IsPortalEnabled = true];
            Account a = [select Id, RecordTypeId from Account where Id = :u.AccountId ];
            RecordType accrt = [select Name FROM RecordType where Id = :a.RecordTypeId];
            l.PortalUser__c = u.Id;
        l.PortalAccount__c = u.AccountId;
        if (accrt.Name == 'Distributor'){
            l.DistiAccount__c = l.PartnerAccountId ;
            
            
        }
        
        }else {
            l.PortalUser__c = l.OwnerId;
            l.PortalAccount__c = l.PartnerAccountId;
        }
        update l;
    }
}


********test**********

@IsTest
private class TestXXCSRLeadConvertClass{
 
        static testMethod void TestXXCSRLeadConvertClass() {
       
       test.startTest();
           Boolean bolCreateOpp;
           String strAccountId;
           String strContactId;
            
            User usr = [select Id,PartnerApprover__c,AccountId from User where  Name='Tim House' and IsPortalEnabled = true];
            RecordType rt = [select Id,Name FROM RecordType where Name='Deal Registration'];
            Lead l = new Lead();
            l.FirstName = 'CRM Testing First';
            l.LastName = 'CRM Testing Last';
            l.Company = 'CRM Testing INCtest';
            l.description = 'Test descr';
            l.city = 'test';
            l.street = 'test';
            l.state = 'CA';
            l.country = 'United States';
            l.status = 'Qualified';
            l.email = 'test@testnetgear.com';
            l.website = 'www.testcrm.com';
            l.ApprovalStatus__c='Approved';
            l.RecordTypeId= rt.Id;
            l.OwnerId = usr.Id; //Timhouse
            
                
              
         
            insert l;
         //update
       
        l = [select Id,OwnerId,PartnerAccountId,RecordTypeId,DistiAccount__c,PortalUser__c ,PortalAccount__c from Lead where  Id = :l.Id];
      
           // system.debug("Owner"+l.OwnerId);
         //    system.debug("AccountId"+usr.AccountId);
            if (rt.Name == 'DealRegistration'){
                 User usr1 = [select Id ,AccountId from User where Id = :l.OwnerId and AccountId  != '' and ContactId != '' and IsPortalEnabled = true];
                Account a = [select Id, RecordTypeId from Account where Id = :usr1.AccountId ];
                RecordType accrt = [select Name FROM RecordType where Id = :a.RecordTypeId];
                // user usr1  = [select Id,AccountId from User where Id = :l.OwnerId];
                // Account a = [select Id, RecordTypeId from Account where Id = :usr1.AccountId and IsPartner = true];
                // RecordType accrt = [select Name FROM RecordType where Id = :a.RecordTypeId];
                 system.assertEquals(l.OwnerId,usr1.Id );
                system.assertEquals(l.PartnerAccountId,usr1.AccountId);
            
                if (accrt.Name == 'Distributor'){
                     system.assertEquals(l.PartnerAccountId ,usr1.AccountId);
                }
            }
            
            Id leadId = l.Id;
        bolCreateOpp = false;
        //Create a reference to the VF page
        PageReference pageRef = Page.XXCSR_LEAD_CONVERT;
        Test.setCurrentPageReference(pageRef);
    
           //Create an instance of the controller extension and call the autoRun method.
        //autoRun must be called explicitly even though it is "autoRun".
        ApexPages.StandardController sc = new ApexPages.standardController(l);
        XXCSRLeadConvertClass leadconvt = new XXCSRLeadConvertClass(sc);
        leadconvt.autoRun();
         //String nextPage = sc.save().getUrl();
        
       
       List<SelectOption> testacct = new List<SelectOption>();
       testacct = leadconvt.getlstCompanyInfo();
        system.debug(testacct);
     
        
     List<SelectOption> testcon = new List<SelectOption>();
       testcon = leadconvt.getlstContactInfo();
        system.debug(testcon);
        
         leadconvt.doNothing();
        
         leadconvt.strAccountId= '1';
         leadconvt.strcontactId= '1';
         leadconvt.convertlead();
        
        
             //Retrieve the converted Lead info and the opps and roles.
            l = [select Id, IsConverted, ConvertedAccountId, ConvertedContactId ,Status, ApprovalStatus__c,
             Company,OwnerId,firstname,lastname,city,country from Lead where Id = :leadId];
          
            
           
        //  system.assert(!l.IsConverted,'Lead Converted' );  
          test.stopTest();
         

        }
   
         

}

Thank you for your help,

Pallavi
 
Gaurav NirwalGaurav Nirwal
@isTest
public class UnitTests_CreateUpdateTask01 {



   static testMethod void CreateUpdateTask01() {

       test.startTest();

      List<Lead> newLeadMap = new List<Lead>();
      List<Lead> oldLeadMap = new List<Lead>();

       Lead l = new Lead(
            FirstName='Test firstName',
            LastName = 'Test LastName',
            Comments__c = '28888 test comments new'
            );

       insert l;
       System.debug('Created and inserted lead');


        Lead 10l=[select id,Comments__c from Lead where id=:l.id limit 1];

        10l.Comments__c = '28888 test comments new change to update';

        update 10l;

       //Assign the context before and after the change into a Map
       // Declare method variables
       Map<Id,Lead> mapVerifyOldLead = new Map<Id, Lead>();

       // Create initial Map of o ld Lead
       for(lead leadOld : oldLeadMap) { mapVerifyOldLead.put(leadOld.Id, leadOld); }

       //Loop through the 1newLeadMap and check old values
       for(Lead leadNew : newLeadMap) {

       if ((newLeadMap.Comments__c<> oldLeadMap.Comments__c)&&(newLeadMap.Comments__c !=null)){
       Tasks.add(new Task(
          ActivityDate = Date.today().addDays(1),
          WhoId = l.Id,
          Status = 'Not Started',
          type='Other',
          Priority='Normal',
          Subject='trigger Comment changed',
          Description = l.Comments__c
       ));

       insert Tasks;
       System.debug('Created and inserted task based on existing lead comment update');

       Task Ttest = [SELECT Whoid, Description FROM Task WHERE Whoid=:l.Id Limit 1];

       system.assertEquals('28888 test comments update', Ttest.Description);

       test.stopTest();

   }

}

}
Pallavic14Pallavic14
Hi Mathew,

I dont have much experience with Apex. Do you want to look my test code and suggest please.
developer console shows that following lines of code from trigger is not covered.

  User u = [select Id ,AccountId from User where Id = :l.OwnerId and AccountId  != '' and ContactId != '' and IsPortalEnabled = true];
            Account a = [select Id, RecordTypeId from Account where Id = :u.AccountId ];
            RecordType accrt = [select Name FROM RecordType where Id = :a.RecordTypeId];
            l.PortalUser__c = u.Id;
        l.PortalAccount__c = u.AccountId;
        if (accrt.Name == 'Distributor'){
            l.DistiAccount__c = l.PartnerAccountId


Thanks
Pallavi