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
niharnihar 

Any one can write a test class for my trigger

trigger TLClusterUpdate on Account (before insert, before update) {
    set<Id> tseIds = new Set<Id>();
    set<Id> tlIds = new set<Id>();
    list<TSE_Master__c> updateTseMasters = new list<TSE_Master__c>();
    list<Outlets_Groups_Primary_Customer__c> groupOutlets = new
list<Outlets_Groups_Primary_Customer__c>();
    if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore){
    for(Account a : Trigger.new){
        if(a.TSE_Code__c != NULL) tseIds.add(a.TSE_Code__c);
       if(a.Group_Name1__c!= NULL) tlIds.add(a.Group_Name1__c);
    }    
    updateTseMasters = [SELECT Id, Name, TL_Code__c,TL_Code__r.cluster__c from
TSE_Master__c where Id IN: TseIds];
        groupOutlets = [Select Id, Name,Lead_TL__c from  
Outlets_Groups_Primary_Customer__c where Id IN: tlIds];
    for(Account acc: Trigger.New){
        for(TSE_Master__c ts: updateTseMasters){
            acc.TL_Code__c = ts.TL_Code__c;
            acc.Cluster_Code__c = ts.TL_Code__r.Cluster__c;
        }
        for(Outlets_Groups_Primary_Customer__c grps: groupOutlets){
           acc.Group_Lead_TL__c = grps.Lead_TL__c;
        }
    }
    }
}
Hemant_SoniHemant_Soni
Hi,
Please try below code for test class.
@IsTest
public class TLClusterUpdateTest{
    
    static testmethod void testBefore(){
        Account oAccount = new Account();
        oAccount.Name = 'Test Account';
        oAccount.TSE_Code__c = 'Test TSE Code';
        oAccount.Group_Name1__c = 'Test Group Name';
        // All Other Required Field
        insert oAccount;

oAccount.TSE_Code__c = 'Test TSE Code 11';
update oAccount;
    }
}
Please let me know if you are facing any issue.
Thanks
Hemant
 
Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm


You write a test class for this the same way that you would any other:
- Set up some data for the Trigger to access
- Verify the behaviour with asserts.

Try below sample test class.
Please try below test class
@IsTest
public class TLClusterUpdateTest
{
    static testmethod void testBefore()
	{
	
		TSE_Master__c mast = new TSE_Master__c ();
		mast.Name ='Test';
		// Add all required field
		insert mast;
		
		
		
		Account oAccount = new Account();
		oAccount.Name = 'Test Account';
		oAccount.TSE_Code__c  = mast.id;
		//oAccount.TSE_Code__c = 'Test TSE Code';
		//oAccount.Group_Name1__c = 'Test Group Name';
		// All Other Required Field
		insert oAccount;

		oAccount.TSE_Code__c = 'Test TSE Code 11';
		update oAccount;
    }
}

Let us know if this will help u