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
System Admin 949System Admin 949 

test trigger problem

Dear community,
below is the my trigger,iam facing some problem for creation of test class.how to create the testfactory for this trigger
trigger:
trigger CheckDefaultPlantContact on PlantContact__c (before insert,before update)
{

    Set<String> contactTypeSet = new Set<String>();
    Set<String> prodFamilySet = new Set<String>();
    Map<String, PlantContact__c> productPlanMap = new Map<String, PlantContact__c>();
    
    For(PlantContact__c p : Trigger.New)
    {
        If(p.TypeOfContact__c != null)
        {
            contactTypeSet.add(p.TypeOfContact__c);
        }
        If(p.ProductFamily__c != null)
        {
            prodFamilySet.add(p.ProductFamily__c);
        }
    }
    
    For(PlantContact__c p :[SELECT Id, TypeOfContact__c,ProductFamily__c FROM PlantContact__c WHERE TypeOfContact__c =:contactTypeSet AND Default__c = TRUE AND ProductFamily__c =: prodFamilySet])
    {
        productPlanMap.put(p.TypeOfContact__c+'-'+p.ProductFamily__c, p);
    }
    
    For(PlantContact__c p : Trigger.New)
    {
        String keyChek = p.TypeOfContact__c+'-'+p.ProductFamily__c;
        If(!productPlanMap.containsKey(keyChek))
        {
            IF(!p.Default__c)
            {
                p.name.addError('Please Mark as Default. There should be one default Contact for type '+ p.TypeOfContact__c);
            }    
        }
            
        IF(p.Default__c)
        {
            
            If(productPlanMap.containsKey(keyChek))
            {
                p.name.addError('One Product Family have only one Default for each '+ p.TypeOfContact__c +' type');
            }
        }
    }
}
can any one provide hoew to create the test class for this.
thanks in advance
Raj VakatiRaj Vakati
Try this code .. add required field and 
 
@isTest
Private Class PlantContactTestClass
{

	static Testmethod void PlantContactTe()
	{
	
		
		Test.startTest();
		
		 PlantContact__c  plan1 = new PlantContact__c () ;
	 plan1.Name ='Test';
	 plan1.TypeOfContact__c ='secondry';
	 plan1.ProductFamily__c ='service';
	 plan1.Default__c  =false ;
	 insert plan1 ;
	 
	 

	 PlantContact__c  plan = new PlantContact__c () ;
	 plan.Name ='Test';
	 plan.TypeOfContact__c ='Primary';
	 plan.ProductFamily__c ='Sales';
	 plan.Default__c  =true ;
	 insert plan ;

	   PlantContact__c  plan11 = new PlantContact__c () ;
	 plan11.Name ='Test';
	 plan11.TypeOfContact__c ='secondry';
	 plan11.ProductFamily__c ='service';
	 plan11.Default__c  =false ;
	 insert plan11 ;
		 
          Test.StopTest();
	}
}

 
Raj VakatiRaj Vakati
try this
 
@isTest
Private Class PlantContactTestClass
{

	static Testmethod void PlantContactTe()
	{
	
		
		Test.startTest();
		
		 PlantContact__c  plan1 = new PlantContact__c () ;
	 plan1.Name ='Test';
	 plan1.TypeOfContact__c ='secondry';
	 plan1.ProductFamily__c ='service';
	 plan1.Default__c  =false ;
	 insert plan1 ;
	 
	 

	 PlantContact__c  plan = new PlantContact__c () ;
	 plan.Name ='Test';
	 plan.TypeOfContact__c ='Primary';
	 plan.ProductFamily__c ='Sales';
	 plan.Default__c  =true ;
	 insert plan ;

	   PlantContact__c  plan11 = new PlantContact__c () ;
	 plan11.Name ='Test';
	 plan11.TypeOfContact__c ='secondry';
	 plan11.ProductFamily__c ='service';
	 plan11.Default__c  =false ;
	 insert plan11 ;
	 
	 
	  plan11.Name ='Test12';
	 plan11.TypeOfContact__c ='None';
	 plan11.ProductFamily__c ='EX';
	 plan11.Default__c  =true ;
	 update  plan11 ;
		 
          Test.StopTest();
	}
}