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 

Help me to create trigger Test Class

Hi Folks,

 

Please help me to write Test class for Trigger Class below.

 

 

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;

}

 }

 }

 

Thanks in advance

 

Thanks & Regards

Srinivas

SennahSennah

I suggest you figured out how to write tests because I saw a recent posting from you that discusses a test class...

 

But why do you wrote a trigger for this anyway? It looks like you can achieve the same by "field mapping" under "customize" -> "leads" -> "fields".

 

//Hannes

WesNolte__cWesNolte__c

Hey

  

// Create your contact

 

Contact c = new Contact;

 

// Create at least 2 Leads, one that is converted and one that isn't

 

Lead l1 = ...;

Lead l2 = ...;

 

l1.convertedleadcontactid = c.id; 

 

insert l1;

insert l2;

 

// Change something in each lead

 

l1.name = ...;

l2.name = ...;

 

update l1;

update l2;

 

// Check that the contact has been update by the trigger

 

System.assert(contact.email==l1.email);

 

*Depending on the amount of data in your system, I would delete all contacts and leads within your test first. Note: This does not permanently delete data, only for the duration of the test, and give you control over the test environment. eg. before all the previous code write,

 

List<Lead> leads = [SELECT id FROM lead];

List<Contact> contact = [SELECT id FROM contact];

 

delete leads;

delete contact;

 

Wes 

 

 

 

csrsakcsrsak

Hi Wes,

 

Thanks for quick reply,

 

i wrote the trigger test class is like,

 

/**
  Test class for TriggerAfterLeadConversion
 */
 
@isTest

private class TriggerAfterLeadConversionTest
{

    static testMethod void myUnitTest()
    {
    List<Lead> leads = [SELECT id FROM lead];
 List<Contact> contact = [SELECT id FROM contact];
 
    Contact c=new Contact(firstName='Venkata',lastname='Rao',Birthdate=Date.newInstance(1980,10,11),Email='venkat.rao@gmail.com');
 insert c;
 Lead le1=new Lead(Date_of_Birth__c=Date.newInstance(1980,10,11),
Email='venkat.rao@gmail.com', Company='quin',LastName='Rao');
 Lead le2=new Lead(Date_of_Birth__c=Date.newInstance(1983,04,20),
Email='venkat.ch@viz.com', Company='abc soft',LastName='Rao');
 insert le1;
 insert le2;
 le1.FirstName='ramu';
 le2.FirstName='siva';
 update le1;
 update le2;
 System.assert(c.Email==le1.email);
 
 if(le1.ConvertedContactId=='0038000000j0qcV')
 {
   Contact con = [SELECT Id FROM Contact WHERE Contact.Id ='0038000000j0qcV'];           
       con.Birthdate = le1.Date_Of_Birth__c;
       con.Email=le1.Email;           
       update con;
 }
 
 delete leads;
    }
}

 

when i run the above test class, it is showing 4 lines not tested, 33% coverd,

 

the 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;

}

}

}

 

the red colors lines in the trigger are not covered,

 

can you please help me to coverage of 75% of the above trigger?

 

Thanks in Advance,

 

 

Thanks and Regards,

 

Srinivas  

WesNolte__cWesNolte__c

Hey

 

One of your leads needs to be 'converted'. I don't know leads that well but looking at the schema it seems that you'd need to set the 'isConverted' field on one of the leads to true when you insert it. Give that a bash..

 

Wes