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
ashu 6112ashu 6112 

Need your help guys

Hi, There are 2 custom objects, Abc and Def. There is a field on Def object, named number. The values in Number field must be unique(Not duplicate) for each the Abc object. I wrote the below code and it is working fine, now I need to write the test class for this trigger. Below is the trigger code:

trigger duplicatenumberWithSameAbc on Def__c (before insert, before update)
{   
    if(Trigger.isBefore && Trigger.isInsert)
    {
            duplicatenumberWithSameAbc dup=new duplicatenumberWithSameAbc();
            dup.insertDef(trigger.new);
    }   
        
    if(Trigger.isBefore && Trigger.isUpdate)
    {
        for(Def__c tb: Trigger.new)
        {
            if(Trigger.oldMap.get(tb.id).number__c != Trigger.newMap.get(tb.Id).number__c)
            {
                duplicatenumberWithSameAbc dup=new duplicatenumberWithSameAbc();
                dup.insertDef(trigger.new);
            }
        }   
    }       
}


Apex class for this trigger:

public class duplicatenumberWithSameAbc 
{
    public void insertDef(List<Def__c> tab1)
    {
        set<Id> stTemp=new set<Id>();
    for(Def__c t:tab1)
    {
        stTemp.add(t.Abc__c);
    }

    Map<id,List<Def__c>> mpDb=new Map<id,List<Def__c>>();
   
    for(Def__c t : [select id,name,Abc__c,number__c from Def__c where Abc__c IN :stTemp])
    {
       
       List<Def__c> tab = new List<Def__c>();
       
       if(mpDb.get(t.Abc__c) != null)
       {
           tab = mpDb.get(t.Abc__c);
           tab.add(t);
           mpDb.put(t.Abc__c , tab);
           
       }
       else{
           List<Def__c> newList = new List<Def__c>();
           newList.add(t);
           mpDb.put(t.Abc__c , newList);
       }
       
    }
    
    for(Def__c t:tab1)
    {
        if(mpDb.containskey(t.Abc__c))
        {
            List<Def__c> tabs = mpDb.get(t.Abc__c);
            
            for(Def__c t1 : tabs)
            {
                if(t.number__c  == t1.number__c  )
                {
                    t.addError('You cannot add duplicate Value in Number Field');
                }
            }
            
        }
    }
}
}
 
Best Answer chosen by ashu 6112
Amit Chaudhary 8Amit Chaudhary 8
Sample Test Factory Class
public abstract class TestDataFactory 
{
    
    public static Abc__c createAbc( ) 
	{
        Abc__c = oAbc = new Abc__c();
		oAbc.name = 'test abc';
		// add data for all required fields
		insert oAbc;
		
		return oAbc;
    }
	
    public static Def__c createDef( Abc__c oAbc) 
	{
		Def__c oDe = new Def__c();
        oDe.Name = 'test account';
		oDe.number__c = 1234;
		oDe.Abc__c  = oAbc.Id;
        // add all required fields
        insert oDe ;
		return oDe;
	}
	
}
Pleass try below test class
@isTest
public class thecontrollerTests 
{
    public static testMethod void testMyController() 
	{
		Abc__c oAbc = TestDataFactory.createAbc();
		Def__c oDe = TestDataFactory.createDef(oAbc);
        
        oDe.number__c = 3216 ;
		update oDe ;
		
		oDe.number__c = 1234 ;
		update oDe ;

    }
}
Please check below post for testFactory in salesforce
1) https://trailhead.salesforce.com/en/apex_testing/apex_testing_data
2) https://developer.salesforce.com/forums/?id=906F000000090j3IAA
3) https://github.com/dhoechst/Salesforce-Test-Factory


Let us know if this will help you
 

All Answers

ashu 6112ashu 6112
Both the objects having master detail relationship with Abc as Parent and Def as Child.
ashu 6112ashu 6112
Hi piyush,

Thanks for this, but I have to create first factory data class and then use it in my test class. Thats the actualt requiremtn. Please help me in this as I dont hav e any idea of factory data class.
Amit Chaudhary 8Amit Chaudhary 8
Sample Test Factory Class
public abstract class TestDataFactory 
{
    
    public static Abc__c createAbc( ) 
	{
        Abc__c = oAbc = new Abc__c();
		oAbc.name = 'test abc';
		// add data for all required fields
		insert oAbc;
		
		return oAbc;
    }
	
    public static Def__c createDef( Abc__c oAbc) 
	{
		Def__c oDe = new Def__c();
        oDe.Name = 'test account';
		oDe.number__c = 1234;
		oDe.Abc__c  = oAbc.Id;
        // add all required fields
        insert oDe ;
		return oDe;
	}
	
}
Pleass try below test class
@isTest
public class thecontrollerTests 
{
    public static testMethod void testMyController() 
	{
		Abc__c oAbc = TestDataFactory.createAbc();
		Def__c oDe = TestDataFactory.createDef(oAbc);
        
        oDe.number__c = 3216 ;
		update oDe ;
		
		oDe.number__c = 1234 ;
		update oDe ;

    }
}
Please check below post for testFactory in salesforce
1) https://trailhead.salesforce.com/en/apex_testing/apex_testing_data
2) https://developer.salesforce.com/forums/?id=906F000000090j3IAA
3) https://github.com/dhoechst/Salesforce-Test-Factory


Let us know if this will help you
 
This was selected as the best answer