You need to sign in to do that
Don't have an account?
Madhulendra
How to write Test Class for after Insert Trigger
hi
can anybody help me to write Test class for this Trigger
trigger insertIntoCustomerContactDetail on Contact (after insert)
{
for (Contact a : Trigger.new)
{
CustomerContactDetail__c ccd = new CustomerContactDetail__c
(
Contact__c = a.id,
First_Name__c =a.firstName,
Last_Name__c=a.lastName,
Date_Of_Birth__c = a.Date_Of_Birth__c,
Mobile_No__c = a.MobilePhone,
Email__c = a.Email__c
);
insert ccd ;}
}
Thanks in Advance
Madhulendra
Hi, you just need create a class with a test method that fires the trigger.
Guessing by your example,
your test method needs to create a Contact Object and then insert it to the database. Code example:
@isTest private class testYourTriggers { static testMethod void testInsertToCustomerContactDetail(){ /* Create a contact and set it's data */ ... insert aContact; ... /* Check that a CustomerConcatacDetail was succefuly created */ } static testMethod void anotherTestMethod(){ ... } }
If you are in a Sandbox or Production, you will need to first save the test class and
then the trigger. Just be sure the test code is activating the trigger.
Hope it helps.
- Zeta
Hi I am having the same problem BUT my trigger is based on the Lead SObject and the trigger calls a handler class. So would this handler class be causing that error problem? Thanks.
Note: the method name for the handler class is: InquiryLeadManager.handleInquiryLeadInsert()
trigger InquiryLeads on Lead (after insert) {
List<Lead> processInquiries = new List<Lead>();
//Record Type for Inquiries
RecordType chi = [Select Id from RecordType where SObjectType = 'Lead' and Name = 'Ch Inquiry'];
List<Lead> leads = [SELECT Id,RecordTypeId,manual_load__c,Inquiry_Packet_Sent__c, MailStatus__c,FirstName,LastName,Street,City,State,County__c,PostalCode FROM Lead WHERE Id IN: Trigger.newMap.keySet()];
for(Lead alead : leads){
system.debug('RECORD TYPE ID: '+alead.RecordTypeId+' STREET: '+alead.Street+' ');
if(alead.MailStatus__c != 'Email'){
if(alead.RecordTypeId == chi.Id && alead.Street!=null && !alead.Street.contains('a href=') && !alead.Street.contains('http://')){
if((alead.MailStatus__c == 'Mail' || alead.MailStatus__c == 'Both')){
//Make sure that no null or blank name contacts are entered from web page.
if(((alead.FirstName != null && alead.LastName != null) || (alead.FirstName != '' && alead.LastName !='')))
{
if(!alead.manual_load__c ){
alead.Inquiry_Packet_Sent__c = true;
processInquiries.add(alead);
system.debug('<< A LEAD>> '+alead);
}
}
}//If
}//If
} //If
}//Loop
if(processInquiries.size() > 0){
update processInquiries;
system.debug('GO TO PROCESS OPPORTUNITIES');
InquiryLeadManager.handleInquiryLeadInsert(processInquiries);
}
}
========= Handler Class ================
public with sharing class InquiryLeadManager {
public class InquiryLeadException extends Exception{}
public static void handleInquiryLeadInsert(List<Lead> newLeads){
List<Opportunity> Orders= new List<Opportunity>();
OrderEntryServices.Result r = new OrderEntryServices.Result();
//Get the Account ID
Account acctId = [Select Id from Account WHERE Mailing_Address_Book__c = '13' LIMIT 1];
//String version of ID Type aID
String aID = String.valueOf(acctId.Id);
//Index counter for Opportunity meaning: 'Opportunity per a specific Lead processed'.
Integer leadCnt = 0;
Boolean foundLead = false;
//Record ID for Order Opportunities
RecordType rec = [Select Id from RecordType where SObjectType = 'Opportunity' and Name = 'Orders'];
//Record Type for Chicago Inquiries
RecordType chi = [Select Id from RecordType where SObjectType = 'Lead' and Name = 'Ch Inquiry'];
// Application will be the Created By user
User ouser = [SELECT Id from User where Name = 'Biz4Applications' limit 1];
system.debug('<<AID> '+aID);
// Get the contact ID to create the opportunity
Contact contactId = [Select Id, Name, FirstName, LastName, Phone, Email, AccountId, Account.Name,
Account.Account_on_Hold__c, Account.Hold_Comments__c, Account.Undeliverable_Mailing__c, Account.Undeliverable_Shipping__c, Account.Mailing_Address_Book__c,
Account.BillingStreet, Account.BillingCity, Account.BillingPostalCode, Account.BillingState, Account.BillingCountry, Account.Billing_County__c,
Account.ShippingStreet, Account.ShippingCity, Account.ShippingPostalCode, Account.ShippingState, Account.ShippingCountry, Account.Shipping_County__c,
Account.RecordType.Name, Account.Default_Terms__c, Account.Default_Shipping_Address__c, Account.CurrencyISOCode
From Contact where Account.Id =: aID and Active__c = true and Purchasing_Contact__c = true and Billing_Contact__c = true Limit 1];
String cID = String.valueOf(contactId.Id);
try{
for(Lead alead : newLeads){
r = OrderEntryServices.CreateOrder(cID,'Chicago Price Book','Order',System.Today(),'Biz4');
if(r.Success){
//Create address to point to the Inquiry address and contact.
foundLead = true;
Orders.add((Opportunity)r.ObjectOutput);
Address__c addr = new Address__c(First_Name__c = alead.FirstName,
Last_Name__c = alead.LastName,
Default_Shipping_Address__c = true,
Address_Line_1__c=alead.Street,
City__c=alead.City,
State__c= alead.State,
Zip_Code__c=alead.PostalCode,
Country__c = 'UNITED STATES',
County__c = alead.County__c,
Show_in_Order_Entry__c = true);
insert addr;
system.debug('<<Orders>> '+Orders);
OrderEntryServices.AddProduct(Orders[leadCnt].Id, '94239', 1, 0, 'S - Stock Inventory Item');
OrderEntryServices.AddProduct(Orders[leadCnt].Id,'43490',1,2.10,'F - Freight');
Orders[leadCnt].Amount = 2.10;
//Move Save and submission as the last step
OrderEntryServices.SaveOrder(Orders[leadCnt]);
system.debug('<<SAVED ORDER>> '+Orders[leadCnt]);
system.debug('<<A-Shipping2>> '+ addr);
Orders[leadCnt].RecordTypeId = rec.Id;
Orders[leadCnt].Type = 'Shipping';
Orders[leadCnt].Order_Type__c = 'SO';
Orders[leadCnt].Shipping_Address__c = addr.Id;
Orders[leadCnt].Ship_to_Name__c = addr.First_Name__c +' '+addr.Last_Name__c;
Orders[leadCnt].Ship_to_First_Name__c = addr.First_Name__c;
Orders[leadCnt].Ship_to_Last_Name__c = addr.Last_Name__c;
Orders[leadCnt].Shipping_Street__c = alead.Street;
Orders[leadCnt].Shipping_City__c = alead.City;
Orders[leadCnt].Shipping_State__c = alead.State;
Orders[leadCnt].Shipping_Zip_Code__c = alead.PostalCode;
Orders[leadCnt].Shipping_Country__c = 'UNITED STATES';
Orders[leadCnt].Inquiry_ID2__c = aLead.Id;
system.debug('<<Order ID>> '+Orders[leadCnt]);
Orders[leadCnt].OwnerId = ouser.Id;
//Now Submit Everything...
OrderEntryServices.SubmitOrder(Orders[leadCnt].Id);
leadCnt++;
}//If
else{
system.debug('Error when creating an opportunity');
}
}//FOR LOOP
if (Orders.size() > 0){
upsert Orders;
System.debug('<<ORDERS>> '+Orders);
}
}//Try
catch(Exception e){
}//Catch
}//handleInquiryLeadInsert()
I forgot to include my test case that failed:
@isTest
private class InquiryLeadsTestCase {
static testMethod void TestIquiryLeadInsert()
{
RecordType chi = [Select Id from RecordType where SObjectType = 'Lead' and Name = 'Chicago Inquiry'];
Test.startTest();
List<Lead> ldNew = new List<Lead>();
ldNew.add(new Lead(FirstName= 'Racer',LastName = 'X',Street= '720 S Michigan Ave',City = 'Chicago',State = 'IL',MailStatus__c = 'Mail',PostalCode = '60601',
Country = 'UNITED STATES',Inquiry_Packet_Sent__c = true,Phone='(312) 555-2000',RecordTypeId=chi.Id));
insert ldNew;
Test.stopTest();
}
}