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
kROOMkROOM 

I am new to coding and need help in making a test class. This class will automatically copy the mailing address to the billing address one the country is USA, US, or CA and will pop an error for the phone if area code is not set to +63 if country is Ph

public class AccountTriggerHandlerEx3 {

    public static void onBeforeInsert(List<Account> accountList){
       populateOpp(accountList);
    }
       
    public static void populateOpp(List<Account> accountList){
        for(Account acc : accountList){
            if(acc.shippingAddress == Null){
                if(acc.BillingCountry == 'US' || acc.BillingCountry == 'USA' || acc.BillingCountry == 'CA'){
                           acc.ShippingStreet             = acc.BillingStreet;
                        acc.ShippingCity               = acc.BillingCity;
                        acc.ShippingState             = acc.BillingState;
                        acc.ShippingPostalCode         = acc.BillingPostalCode;
                        acc.ShippingCountry         = acc.BillingCountry;
            }
        }
            if(acc.BillingCountry == 'PH' || acc.ShippingCountry == 'PH'){
            if (acc.Phone != Null || acc.phone != ''){
                if(acc.Phone.substring(0,3) != '+63'){
                    acc.Phone.addError('Philippine telephone numbers must be prefixed with the Country Code +63');
                }
                           
            }  
            }
        
        if(acc.BillingCountry == 'PH' || acc.ShippingCountry == 'PH'){
            if (acc.Fax != Null || acc.Fax !=''){
                if(acc.Fax.substring(0,3) != '+63'){
                    acc.Fax.addError('Philippine Fax numbers must be prefixed with the Country Code +63');
                }
                           
            }  
            }}
    }


      public static void onAfterUpdate(List<Account> accountSet){
       populateOppp(accountSet);
    }

    private static void populateOppp(List<Account> accountSet){
        List<Id> accountIds = new List<Id>();
        for(Account acc : accountSet){
          
        Account old = (Account)trigger.oldMap.get(acc.Id);
        if (acc.billingstreet != old.billingstreet|| acc.billingcity !=old.billingcity || acc.billingstate !=old.billingstate || acc.billingcountry != old.billingcountry) {
            accountIds.add(acc.Id);
        }
            
        }  
        if (accountIds.size() > 0) {
        contact[] updates = [select Id, accountId from contact where accountId in :accountIds];
        for (contact c : updates) {
            Account a = (account)trigger.newmap.get(c.accountId);
            c.mailingStreet = a.billingStreet;
            c.mailingcity = a.billingcity;
            c.mailingstate = a.billingstate;
            c.mailingpostalcode = a.billingpostalcode;
            c.mailingcountry = a.billingcountry;
                    }
        update updates;
    }                                                   
    
    
    }
    

}
Best Answer chosen by kROOM
Raj VakatiRaj Vakati
Try this code
 
@isTest
private class AccountTriggerHandlerEx3Test {
    @isTest static void testCaseOne() {
    
	
	Account newAccount = new Account (
name='XYZ',
Phone = '+1-1212121212',
BillingStreet = 'Am Tierpark 16',
BillingCity = 'Santa Clara',
BillingPostalCode = '95051',
BillingState = 'CA',
BillingCountry = 'USA'
);
insert newAccount;
		
		
		
		
			Account newAccountPH = new Account (
name='XYZ',
Phone = '+63-1212121212',
Fax = '+63-1212121212',
BillingStreet = 'Am Tierpark 16',
BillingCity = 'Santa Clara',
BillingPostalCode = '95051',
BillingState = 'CA',
BillingCountry = 'PH'
);
insert newAccountPH;
		
		
		
		newAccount.name='1212';
newAccount.Phone = '+1-12387213';
newAccount.BillingStreet = 'Am Tierpark 16';
newAccount.BillingCity = 'San Jose';
newAccount.BillingPostalCode = '95851';
newAccount.BillingState = 'CA';
newAccount.BillingCountry = 'USA';
		update newAccount ;
	
	
    }
    
}

 

All Answers

Raj VakatiRaj Vakati
Try this code
 
@isTest
private class AccountTriggerHandlerEx3Test {
    @isTest static void testCaseOne() {
    
	
	Account newAccount = new Account (
name='XYZ',
Phone = '+1-1212121212',
BillingStreet = 'Am Tierpark 16',
BillingCity = 'Santa Clara',
BillingPostalCode = '95051',
BillingState = 'CA',
BillingCountry = 'USA'
);
insert newAccount;
		
		
		
		
			Account newAccountPH = new Account (
name='XYZ',
Phone = '+63-1212121212',
Fax = '+63-1212121212',
BillingStreet = 'Am Tierpark 16',
BillingCity = 'Santa Clara',
BillingPostalCode = '95051',
BillingState = 'CA',
BillingCountry = 'PH'
);
insert newAccountPH;
		
		
		
		newAccount.name='1212';
newAccount.Phone = '+1-12387213';
newAccount.BillingStreet = 'Am Tierpark 16';
newAccount.BillingCity = 'San Jose';
newAccount.BillingPostalCode = '95851';
newAccount.BillingState = 'CA';
newAccount.BillingCountry = 'USA';
		update newAccount ;
	
	
    }
    
}

 
This was selected as the best answer
Raj VakatiRaj Vakati
Use this code pls
 
@isTest
private class AccountTriggerHandlerEx3Test {
    @isTest static void testCaseOne() {
    
	
	Account newAccount = new Account (
name='XYZ',
Phone = '+1-1212121212',
BillingStreet = 'Am Tierpark 16',
BillingCity = 'Santa Clara',
BillingPostalCode = '95051',
BillingState = 'CA',
BillingCountry = 'USA'
);
insert newAccount;
		
		
	
        Contact c=new Contact(
            FirstName='fname',
            LastName = 'lname',accountid=newAccount.id,
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert c;

		
		
			Account newAccountPH = new Account (
name='XYZ',
Phone = '+63-1212121212',
Fax = '+63-1212121212',
BillingStreet = 'Am Tierpark 16',
BillingCity = 'Santa Clara',
BillingPostalCode = '95051',
BillingState = 'CA',
BillingCountry = 'PH'
);
insert newAccountPH;
		
		
		
		newAccount.name='1212';
newAccount.Phone = '+1-12387213';
newAccount.BillingStreet = 'Am Tierpark 16';
newAccount.BillingCity = 'San Jose';
newAccount.BillingPostalCode = '95851';
newAccount.BillingState = 'CA';
newAccount.BillingCountry = 'USA';
		update newAccount ;
	
	
    }
    
}

 
kROOMkROOM
This test helped me a lot.. Appreciated it. Thank you.