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
salesforce sfdxsalesforce sfdx 

How to write a test Class for this Sample Apex Program in salesforce

public class AccounZipCodeTriggerHandler {

    public  Id SYS_ADMIN_PROFILE_ID{get;set;}
    
  public void afterInsertMethod(List<account> NewAcc){
     //SYS_ADMIN_PROFILE_ID =  [select ProfileId from User where Profile.Name = 'System Administrator' limit 1].ProfileId;
     //Business Logic for After Insert functionality
     Id AccRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('ITEmploye').getRecordTypeId();
         If(!NewAcc.isEmpty()){
         For(Account acc : NewAcc){
             If(acc.RecordTypeId == AccRecordTypeId){
                 If(acc.BillingPostalCode!= Null){
                      List<Zip_Code__c> Zipcodlist = [Select Territory__c from Zip_Code__c where Name =:acc.BillingPostalCode  ];
                 If(!Zipcodlist.isEmpty()){
                    acc.Territory_LU__c =Zipcodlist[0].Territory__c;  //assigning the value to the Account territory
                   //insert acc; 
                 }
                 
                 }
             } 
         }
         
         
     }
  }
    public void afterUpdateMethod(List<account> UpdateAcc){
          system.debug('Update Method');
       SYS_ADMIN_PROFILE_ID =  [select ProfileId from User where Profile.Name = 'System Administrator' limit 1].ProfileId;
       //If(UserInfo.getProfileId() != SYS_ADMIN_PROFILE_ID ){
     //Business Logic for After Insert functionality
     Id AccRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('ITEmploye').getRecordTypeId();
        If(!UpdateAcc.isEmpty()){
            For(Account acc : UpdateAcc){
                If(acc.RecordTypeId == AccRecordTypeId){
               //if billing postalcode not null and Territory value is null
             If( acc.BillingPostalCode!= Null && acc.Territory_LU__c == NULL && acc.Run_Apex__c == FALSE ){
                 system.debug('Entered in 1');
                  List<Zip_Code__c> Zipcodlist = [Select Territory__c from Zip_Code__c where Name =:acc.BillingPostalCode AND Territory__c!=null ];
                       If(!Zipcodlist.isEmpty()){
                        acc.Territory_LU__c =Zipcodlist[0].Territory__c;  //assigning the value to the Account territory
                       
                       }
             }else If(acc.BillingPostalCode!= Null && acc.Territory_LU__c != NULL && acc.Run_Apex__c == FALSE){
                  system.debug('Entered in 2');
                    List<Zip_Code__c> Zipcodlist = [Select Territory__c from Zip_Code__c where Name =:acc.BillingPostalCode AND Territory__c!=null];
                //if the user is not system administrator
                 If(UserInfo.getProfileId() != SYS_ADMIN_PROFILE_ID ){
                   If(!Zipcodlist.isEmpty()){
                      If(Zipcodlist[0].Territory__c !=  acc.Territory_LU__c ){
                          acc.addError('Address changes are not allowed'); 
                      }
                   }  
                 //if the user is a system administrator
                }else If(UserInfo.getProfileId() == SYS_ADMIN_PROFILE_ID){
                         If(!Zipcodlist.isEmpty()){
                           acc.Territory_LU__c =Zipcodlist[0].Territory__c;  //assigning the value to the Account territory
                        }
                     }
             
             }else If(acc.Run_Apex__c ==TRUE && acc.BillingPostalCode != NULL){
                   system.debug('Entered in 3');
                  List<Zip_Code__c> Zipcodlist = [Select Territory__c from Zip_Code__c where Name =:acc.BillingPostalCode AND Territory__c!=null];
                   
                  If(!Zipcodlist.isEmpty()){
                
                        acc.Territory_LU__c =Zipcodlist[0].Territory__c;  //assigning the value to the Account territory
                        acc.Run_Apex__c=FALSE; //this is code to make true to false
                   }
              }
          }
              
        }
     }
   }
}

Trigger Handler:
 
trigger AccountZipCodeTrigger on Account (before insert , before update) {
    
   AccounZipCodeTriggerHandler  AccZipHandler = New AccounZipCodeTriggerHandler();
    //Before insert 
   if(Trigger.isBefore && Trigger.isinsert){
	 AccZipHandler.afterInsertMethod(Trigger.New);   
   }
    
    //Before update 
    if(Trigger.isBefore && Trigger.isupdate){
       AccZipHandler.afterUpdateMethod(Trigger.New);
    }
    
}

Thanks
Prathushya Reddy
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Prathusha,

Did you try writing a test class by just creating Account records and updating them or do you need entire test class?

Thanks,
 
salesforce sfdxsalesforce sfdx
hi praveen
iam a fresher just need to coverthe test class do you need entire test class
Thanks for your reply