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
MadhulendraMadhulendra 

Getting problem to wirte Test Class for this Trigger

I am getting problem to write Test class for given trigger can anybody help me

 

here is my trigger

 

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

Madhulendra

jkucerajkucera

For the test you need to create a lead, insert it, convert it (and create a new contact, oppty, and account), query the contact for the birthdate & email fields & do a system assert that they are as expected.

 

Details on how to convert a lead in code are in the apex guide & the API guide:

http://www.salesforce.com/us/developer/docs/apexcode/index.htm

 http://www.salesforce.com/us/developer/docs/api/index.htm

 

This Java code from the API docs is the closest thing to Apex so you can probably just tweak this:

 

private Boolean convertLead (String leadId, String contactId, 
String accountId, boolean overWriteLeadSource, boolean doNotCreateOpportunity,
String opportunityName, String convertedStatus, boolean sendEmailToOwner)
{
LeadConvert leadConvert = new LeadConvert();
leadConvert.setLeadId(new ID(leadId));
leadConvert.setContactId(new ID(contactId));
leadConvert.setAccountId(new ID(accountId));
leadConvert.setOverwriteLeadSource(overWriteLeadSource);
leadConvert.setDoNotCreateOpportunity(doNotCreateOpportunity);
leadConvert.setOpportunityName(opportunityName);
leadConvert.setConvertedStatus(convertedStatus);
leadConvert.setSendNotificationEmail(sendEmailToOwner);
LeadConvertResult[] lcr = null;
try {
lcr = binding.convertLead(new LeadConvert[] {leadConvert});
for (int i=0; i<lcr.length; i++) {
if (lcr[i].isSuccess()) {
System.out.println("Conversion succeeded.\n");
LeadConvertResult result = lcr[i];
System.out.println("The new contact id is: " + result.getContactId());
} else {
System.out.println("The conversion failed because: " + lcr[i].getErrors(0).getMessage());
}
}
} catch (UnexpectedErrorFault e) {
System.out.println("Unexpected error encountered:\n\n" + e.getExceptionMessage());
return false;
} catch (RemoteException e) {
System.out.println("Remote exception encountered:\n\n" + e.getMessage());
return false;
}
return true;
}