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 

How to write Test class for Trigger

I am unable to write Test Class for Trigger.

 

here is my trigger

 

this trigger fire after record inserted into Contact Object and also insert record to

CustomerContactDetails custom object. can anybody help me.

 

 

trigger insertIntoCustomerContactDetail on Contact (after update)

{

 

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.Birthdate,

Mobile_No__c = a.MobilePhone,

Email__c = a.Email

);

insert ccd ;

}

}

 

Thanks in advance

Madhulendra

Best Answer chosen by Admin (Salesforce Developers) 
Kirtesh_JainKirtesh_Jain
Hi in testmethod ,  insert a contact , after it update that contact..your trigger will be called ..and will be tested.

All Answers

Kirtesh_JainKirtesh_Jain
Hi in testmethod ,  insert a contact , after it update that contact..your trigger will be called ..and will be tested.
This was selected as the best answer
MadhulendraMadhulendra

I have written test class for trigger and inserting a new record

in contact Object but it still showing 0% coverage.

 

private class insertIntoCustomerContactDetailTest

{

 

static testMethod void myUnitTest()

{

Contact a=new Contact();

a.firstName='srinu';a.lastName=

'reddy';

a.Birthdate=Date.newInstance(1984,10,11);

a.MobilePhone='9711099217';

a.Email='svreddych@gmail.com';

insert a;

 

}

}

NaishadhNaishadh

you write trigger for update not for insert.

 

Add following two line and it will work.

 

 

a.firstName='srinu1'; update a;

 

 

 

MadhulendraMadhulendra
thanks, it's working now  :smileyhappy: