+ Start a Discussion
sreekanth reddysreekanth reddy 

How To Write Test Class For Below Trigger

Hi All,
How To Write Test Class For Below Trigger.
trigger create_zapping_rec_Empaccount on Account(before insert)
{
 list<Zapping_Directory__c> zappinglist = new list<Zapping_Directory__c>();
 for(Account c:trigger.new)
 {
  if(c.RecordTypeId=='6666464564')
  {
  Zapping_Directory__c zap = new Zapping_Directory__c(Name=c.Company_Name__c,Company_Type__c=c.Company_Type__c,Main_Industry__c=c.Industrys__c
  );
  zappinglist.add(zap);
  }
  insert zappinglist;
 }
}
Thanks 
Srikanth.
Peter Friberg 26Peter Friberg 26
Hi Srikanth

I would create a test class that:
  • creates a list of accounts with different record types having name, type and industry set
  • insert those accounts
  • read all zapping_directory records using a select statement
  • check all zapping_directory records have a corresponding account with correct recordtype, name, type and industry using System.assertEquals()
  • check that accounts not having recordtype '6666464564' does not have corresponding zapping_directory
ManojjenaManojjena
Hi Sreekanth,

Try with below code it will help!!
@isTest
private class Testcreate_zapping_rec_Empaccount{
   private static testMethod void unitTest(){
     Id recId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('NameOfRecType').getRecordTypeId();
      Account acc=new Account();
	  acc.Name='TestAccount';
	  acc.RecordTypeId=recId;
	  Insert acc;
   }

}
Replace your recordtype name and also don't hard code recordtype id .
You can use below code for recordtype Id dynamically .
Id recId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('NameOfRecType').getRecordTypeId();
Check below link for more detail it wil help !!
http://manojjena20.blogspot.in/2015/08/how-to-get-recordtypeid-in-apex-without.html