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
csrsakcsrsak 

Run tests of test trigger class shows 35% coverage, help me resolve remaining

Hi Friends,

 

I am new into this salesforce.com platform, as of my requirement I did the test class,

 

When I run the test class, it shows 35% of test coverage,

 

Please help me to resolve the remaining.

 

Trigger is:

 

trigger TriggerAfterLeadConversion on Lead (after update)
{
 for(Lead lead:Trigger.new)
 {
  if (Lead.IsConverted)
     {
      
Contact con = [SELECT Id FROM Contact WHERE Contact.Id = :lead.ConvertedContactId];           
       con.Birthdate = lead.Date_Of_Birth__c;
       con.Email=lead.Email;           
       update con;
    
      }
  }
}

 

Trigger test class is,

 

@isTest

private class TriggerAfterLeadConversionTest
{

    static testMethod void myUnitTest()
    {
    Lead le=new Lead(Date_of_Birth__c=Date.newInstance(1984,10,11),
Email='svreddych@gmail.com', Company='Saksoft',LastName='reddy');
 insert le;
 
 Contact con = new Contact(firstName='srinivas',lastName='reddy',Email='svreddych@gmail.com');
 insert con;
 con.Birthdate=le.Date_of_Birth__c;
 con.Email=le.Email;
 update con;
 update le;
 system.assertEquals('Email',le.Email);
 
    }
}

 

When I run tests for test trigger class it shows,

 

The red color code in trigger are not covered,

 

Please help me to resolve this,

 

Thanks in advance,

 

Regards,

Srinivas Chittela

JimRaeJimRae

Your if condition in the trigger code indicates you are looking for a converted lead, but you are not converting the lead in your test method.  You appear to create a lead, then create a contact, this is not the same.  I am not sure of your business requirement.  Once you convert the lead, you wouldn't update it again, it would already be an account, contact and/or opportunity.  Have you tried to simulate the action of your trigger using the UI? I did, and couldn't do it.  Attempting to access the converted lead to update it, brings me to a "this lead is already converted, and here are the new objects" screen.

 

This all being said, if you did really have this requirement, you would need to create the lead, then use the apex code to convert it to a new contact, then update the contact and/ or the lead to get your coverage.

Look in the apex reference under  ConvertLead (Page 178).

lvivaninlvivanin

Try this:

@isTest

private class TriggerAfterLeadConversionTest
{
static testMethod void myUnitTest()
{
Account a = new Account(Name='test NAME');
INSERT a;
Lead le=new Lead(Date_of_Birth__c=Date.newInstance(1984,10,11), Email='svreddych@gmail.com', Company='Saksoft',LastName='reddy');
insert le;
Contact con = new Contact(accountId = a.id,firstName='srinivas',lastName='reddy',Birthdate=le.Date_of_Birth__c,Email=le.Email);
insert con;

Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(le.id);
lc.setAccountId(a.id);
lc.setContactId(con.id);
lc.setConvertedStatus('Closed - Converted');

Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
update le;
}
}

 Though converted lead is not accessable and we get Exception:

System.DmlException: Update failed. First exception on row 0 with id 00Q4000000PWO1wEAH; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead:

 

 

It shows the 100% code coverage for TriggerAfterLeadConversion

 

hope it helps.

csrsakcsrsak

Hi JimRae,

 

Thanks for reply quickly.

 

As per my requirement when I convert lead, then the same data will be update in account, contact, opportunity and Customer Contact details(custom object). For this functionality the trigger is working fine and update the data into Customer contact details.

 

But when I did test trigger class and run tests for that class it isn't coverage of 75%, and shows 35% cover.

 

Still it showing the same.

 

Please help me regarding this..

 

Thanks in advance,

 

Regards,

srinivas chittela

 

 

csrsakcsrsak

Hi Ivivanin,

 

Thnaks for great help..

 

Thanks and Regards,

 

srinivas chittela