You need to sign in to do that
Don't have an account?
kROOM
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;
}
}
}
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;
}
}
}
All Answers