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
Ajay Patel 54Ajay Patel 54 

how to write test class for the trigger

i want to write the test class for the trigger

trigger ChangeRecordTypetoBussinessAccount on Account (before insert) {
  IF(Trigger.Isbefore||Trigger.IsAfter ){
    Map< String,Id> typeMap = New Map< String,Id>();
    for(RecordType rt: [Select DeveloperName, Id From RecordType Where sObjectType = 'Account']) {
         typeMap.put(rt.DeveloperName,rt.Id);
    }
    for (Account cc : trigger.new)  {
       if (cc.Rec_type_Bussiness_Account_hidden__c    == 'Labs Sales' && cc.IsPersonAccount == False) { 
               if(typeMap.containsKey('Business_Account'))
               cc.RecordTypeid = typeMap.get('Business_Account');
       }
   }
  }
}

Best Answer chosen by Ajay Patel 54
ANUTEJANUTEJ (Salesforce Developers) 
Hi Ajay,

You can try the below snippet and modify it as per your use case:
 
@isTest 
public class ChangeRecordTypetoBussinessAccountTest {
    static testMethod void testMethod1() 
    {
		List<Account> alist= new List<Account>();
        Set<id> idset = new set<id>();
        Account a= new Account();
        a.name= 'test';
        a.Rec_type_Bussiness_Account_hidden__c='Labs Sales';
        a.IsPersonAccount=False;
        insert a;
    }
}

Please do note that this is a snippet and you need to modify it as per your usecase.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Ajay,

You can try the below snippet and modify it as per your use case:
 
@isTest 
public class ChangeRecordTypetoBussinessAccountTest {
    static testMethod void testMethod1() 
    {
		List<Account> alist= new List<Account>();
        Set<id> idset = new set<id>();
        Account a= new Account();
        a.name= 'test';
        a.Rec_type_Bussiness_Account_hidden__c='Labs Sales';
        a.IsPersonAccount=False;
        insert a;
    }
}

Please do note that this is a snippet and you need to modify it as per your usecase.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer
Ajay Patel 54Ajay Patel 54

Thanks @ANUTEJ