You need to sign in to do that
Don't have an account?
Yelper Dev
Account and Contact Create Test Class
Hi need help on creating test class for the code below:
The trigger is in before update, already have test factory for this:
Much need help thank you!
public static void copyContactMailingAddress(List<Account> acctList, Map<Id, Account> oldAcctMap){ //Returns all new keys contain in the oldAcctMap Set<Id> acctNewId = Trigger.newMap.keyset(); for (Account acct : acctList){ Account oldAcctRecord = oldAcctMap.get(acct.Id); //Query all contactRecords with their mailing address List<Contact> contactRecord = [SELECT id, accountId, MailingStreet, MailingCity, MailingCountry, MailingPostalCode, MailingState FROM Contact WHERE accountId in : acctNewId]; //Compare values from old to new billing address. for (Contact con : contactRecord) { if (oldAcctRecord.BillingStreet != acct.BillingStreet || oldAcctRecord.BillingCity != acct.BillingCity || oldAcctRecord.BillingState != acct.BillingState || oldAcctRecord.BillingPostalCode != acct.BillingPostalCode || oldacctRecord.BillingCountry != acct.BillingCountry){ con.MailingStreet = acct.BillingStreet; con.MailingCity = acct.BillingCity; con.MailingState = acct.BillingState; con.MailingPostalCode = acct.BillingPostalCode; con.MailingState = acct.BillingCountry; } } //DML Statement to update the Billing Address that is in the Account to all related contacts update contactRecord; } }
The trigger is in before update, already have test factory for this:
@isTest public TestDataFactory{ public static List<Account> createAcctWithCon(Integer numAcct, Integer numConPerAcct){ List<Account> acct = new List<Account>(); for(Integer i = 0; i < numAcct; i++){ Account a = new Account(Name = 'Test Account' + i); acct.add(a); } insert acct; List<Contact> con = new List<Contact>(); for(Account at : acct){ for(Integer j = 0; j < numAcct; j++){ con.add(new Contact (LastName = at.Name + 'Contact' + j, AccountId = at.Id)); } } insert con; return acct; } }
Much need help thank you!
All Answers
And make sure you need to update the record in the test class after you get from the factory class
Also i am getting an error that I cant retreieve the accountId of tempAcctList