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
raji devi 1raji devi 1 

Test class for trigger on contact

The following is my trigger code i written test class for this, i coverd upto 80% i wand to make it 100% can you help me to fulfill this.

trigger createaccount on Contact (before insert, before update) {
string sp =' ';

if(trigger.isinsert && trigger.isbefore ){
    
    
//add error messages to select only accountid or checkbox
        for(contact con : trigger.new){
            if(con.accountid != null && con.Create_New_Account__c ==true){
            
                con.addError('Enter only account name or create account checkbox true');
                
            }
            if(con.firstname == null)
                con.adderror('FirstName is madatory');
                 
        }
        
}

    if(Trigger.isInsert && Trigger.isBefore ){    
        set<id> stid = new set<id>();
        list<contact>con=trigger.new;
        for(contact cn : con)
        {
            if(string.isnotblank(cn.Staging_ID__c) && cn.FirstName!= null && cn.LastName!=null)
            {
                stid.add(cn.Staging_ID__c);
                cn.firstname=cn.FirstName.subString(0 ,1).ToUpperCase()+cn.FirstName.subString(1);
               cn.lastname=cn.LastName.subString(0 ,1).ToUpperCase()+cn.LastName.subString(1);
               }
        }
        
        list<ProfileStaging__c> pslist = [select id , Profile_Created__c from ProfileStaging__c where id in : stid];
        for(ProfileStaging__c ps : pslist)
        {
         
            ps.Profile_Created__c = true;
            
        }update pslist;
    }
   
 
    if(Trigger.isInsert && Trigger.isBefore ){    
       
        
        List<Account> acclist = new List<Account>();
      
        Map<String,Account > accountMap = new Map<String,Account >();
        for(Contact c:Trigger.New){
         if(c.FirstName!= null && c.LastName!=null)
         {
        if(c.Create_New_Account__c == True){
       
        Account acc = new Account();
       // c.idmaping__c=c.id;
        
        acc.name =c.FirstName.subString(0 ,1).ToUpperCase()+c.FirstName.subString(1)+sp+ c.LastName.subString(0 ,1).ToUpperCase()+c.LastName.subString(1)+sp+ 'Family';
        
        acclist.add(acc);
        }
        
        }
        if(!acclist.isEmpty())
        insert acclist;
       
        }
        for(Account acc : acclist) {
            System.debug('accName===>'+acc.name);
            accountMap.put(acc.name,acc);
        }
        for(Contact c:Trigger.New){
            if(c.Source_temp__c != null)
            {
                c.Source__c= c.Source_temp__c;
            }
            if(c.Create_New_Account__c == True) {
                 if(c.FirstName!= null && c.LastName!=null)
                {
               c.AccountId = accountMap.get(c.FirstName.subString(0 ,1).ToUpperCase()+c.FirstName.subString(1)+sp+ c.LastName.subString(0 ,1).ToUpperCase()+c.LastName.subString(1)+sp+ 'Family').id;
                c.Create_New_Account__c = False;
               }
            }
        }
    }
   
}
************************************************************* my test class is*******************************************
@istest
public class triggerAccCreatedwtihFamilyTest{
    static testmethod void unittestmethod(){
Test.startTest();
           
        contact c = new contact();
        c.lastname='test';
        c.firstname = 'testfirtname';
        c.Potential_Non_Potential__c ='p';
        c.Personal_Email__c ='raji@gmail.com';
        c.Create_New_Account__c=true;
        c.Pincode__c= '500045';
         insert c;
        
   Test.stopTest();
           
        }
    
}
sandeep sankhlasandeep sankhla
Hi,

You need to check which lines are not covering then accordingly you can write tst code to cover those lines...

Like youc an insert conatct where Account Id and checkbox is not null...and then you can insert one more record where these avalues are null so it will cover both the parts..

in your test code you haev not added Acount adn in code you are chceking in below code if accountId is not null then only it will go inside..
so to cover this condition you can insert one conatct witha ccount id so ot will cover that part likely you can do for rest.
 
Account objAccount = new Account(name = 'Test Account');
Insert objAccount;


Contact objContact = new Contact();
objContact.AccountId = ObjAccount.Id;
objContact.Create_New_Account__c = true;
objContact.LastName = 'TestlastName';

insert objContact;

please check adn let me lknow if that helps you,

Thanks,
Sandeep
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class to get more then 90% code coverage
@istest
public class triggerAccCreatedwtihFamilyTest
{
    static testmethod void unittestmethod()
	{
		Test.startTest();
		   
			contact c = new contact();
			c.lastname='test';
			c.firstname = 'testfirtname';
			c.Potential_Non_Potential__c ='p';
			c.Personal_Email__c ='raji@gmail.com';
			c.Create_New_Account__c=true;
			c.Pincode__c= '500045';
			insert c;

		Test.stopTest();
           
    }

    static testmethod void unittestmethod1()
	{
		ProfileStaging__c ProfileStaging= new ProfileStaging__c();
		ProfileStaging.name ='demo';
		// add all required field here
		insert ProfileStaging;
	
	
		Test.startTest();
		   
			contact c = new contact();
			c.Staging_ID__c =ProfileStaging.id;
			c.lastname='test';
			c.firstname = 'testfirtname';
			c.Potential_Non_Potential__c ='p';
			c.Personal_Email__c ='raji@gmail.com';
			c.Create_New_Account__c=true;
			c.Pincode__c= '500045';
			insert c;

		Test.stopTest();
           
    }

    static testmethod void unitError()
	{
		Account acc = new Account();
		acc.Name='demo';
		insert acc;
		
		Test.startTest();
		
		try
		{	
			contact c = new contact();
			a.accountid =acc.id
			c.lastname='test';
			c.firstname = 'testfirtname';
			c.Potential_Non_Potential__c ='p';
			c.Personal_Email__c ='raji@gmail.com';
			c.Create_New_Account__c=true;
			c.Pincode__c= '500045';
			insert c;
		}
		Catch(Exception ee)
		{
		}
		Test.stopTest();
           
    }
	
    static testmethod void unitError1()
	{
		Test.startTest();
		
		try
		{	
			contact c = new contact();
			c.lastname='test';
			c.Potential_Non_Potential__c ='p';
			c.Personal_Email__c ='raji@gmail.com';
			c.Create_New_Account__c=true;
			c.Pincode__c= '500045';
			insert c;
		}
		Catch(Exception ee)
		{
		}
		Test.stopTest();
    }
    
}
Let us know if this will help you

Thanks
Amit Chaudhary