I'm really new to this platform (one week barely, although the programming logic is the same for everything) and I'm not able to solve this.
I have added two new choices to the Type picklist of the Account object "First" and "Last", When a pick "First" I need to set the Account Address from the first contact related to that account, and when "Last" the last address from the last contact related to that account.
This is what the senior Developer ask me to do:
Create two Types in account = FIRST and LAST.
I need something to pass the contact information to the account (mailing = billing).
- If the type of account is FIRST -> it will take the information of the first contact related to the account. The first contact is always the first one created, the account will always take the information from it.
- If the type of account is LAST -> it will take the information of the last contact related to the account. The last contact is always the last one created, the account will always take the information from it.
This was my previous code
trigger UpdateAddress on Account (before insert, before update) {
Set<String> firstAccountIds = new Set<String>();
Set<String> lastAccountIds = new Set<String>();
List<Account> accList = new List<Account>();
List<Contact> contactList = new List<Contact>();
Account acc;
Map<String, String> mapBillingStreet = new Map<String, String>();
Map<String, String> mapBillingCity = new Map<String, String>();
Map<String, String> mapBillingState = new Map<String, String>();
Map<String, String> mapBillingCountry = new Map<String, String>();
Map<String, String> mapBillingPostalCode = new Map<String, String>();
for(Account acc : trigger.new) {
if(acc.Type == 'First') {
firstAccountIds.add(acc.Id);
} else if (acc.Type == 'Last') {
lastAccountIds.add(acc.Id);
}
}
for(Contact firstContact: [SELECT Id, AccountId, MailingStreet, MailingCity, MailingState, MailingCountry, MailingPostalCode FROM Contact WHERE AccountId IN :accList ORDER BY CreatedDate ASC LIMIT 1]) {
mapBillingStreet.put(firstContact.AccountId, firstContact.MailingStreet);
mapBillingCity.put(firstContact.AccountId, firstContact.MailingCity);
mapBillingState.put(firstContact.AccountId, firstContact.MailingState);
mapBillingCountry.put(firstContact.AccountId, firstContact.MailingCountry);
mapBillingPostalCode.put(firstContact.AccountId, firstContact.MailingPostalCode);
}
for(Contact lastContact: [SELECT Id, AccountId, MailingStreet, MailingCity, MailingState, MailingCountry, MailingPostalCode FROM Contact WHERE AccountId IN :accList ORDER BY CreatedDate DESC LIMIT 1]) {
mapBillingStreet.put(lastContact.AccountId, lastContact.MailingStreet);
mapBillingCity.put(lastContact.AccountId, lastContact.MailingCity);
mapBillingState.put(lastContact.AccountId, lastContact.MailingState);
mapBillingCountry.put(lastContact.AccountId, lastContact.MailingCountry);
mapBillingPostalCode.put(lastContact.AccountId, lastContact.MailingPostalCode);
}
if(!accList.IsEmpty()){
update accList;
}
}
I came to this cuz a recommendation of a coworker, but it doesn't work and I think is too robust.
Please help, the company is testing me and my job depends on this.
Edit
**This is the actual code:**trigger TestTrigger on Account (after insert, after update) {
List<Account> accList = new List<account>();
Set<Id> firstAcctIds = new Set<Id>();
Set<Id> lastAcctIds = new Set<Id>();
// Separate your "first" type accounts from your "last" type accounts
for(Account acct : Trigger.new){
if(acct.Type == 'First'){
firstAcctIds.add(acct.Id);
} else if (acct.Type == 'Last'){
lastAcctIds.add(acct.Id);
}
}
// Populate the address fields of all the "before" accounts with the values from the contact
for(Contact firstContact: [SELECT Id, AccountId, MailingStreet, MailingCity, MailingState, MailingCountry, MailingPostalCode FROM Contact WHERE AccountId IN :firstAcctIds ORDER BY CreatedDate ASC LIMIT 1]){
Account a = Trigger.newMap.get(firstContact.AccountId);
a.BillingStreet = firstContact.MailingStreet;
a.BillingCity = firstContact.MailingCity;
a.BillingState = firstContact.MailingState;
a.BillingCountry = firstContact.MailingCountry;
a.BillingPostalCode = firstContact.MailingPostalCode;
accList.add(a);
}
// Populate the address fields of all the "last" accounts with the values from the contact
for(Contact lastContact: [SELECT Id, AccountId, MailingStreet, MailingCity, MailingState, MailingCountry, MailingPostalCode FROM Contact WHERE AccountId IN :lastAcctIds ORDER BY CreatedDate DESC LIMIT 1]){
Account a = Trigger.newMap.get(lastContact.AccountId);
a.BillingStreet = lastContact.MailingStreet;
a.BillingCity = lastContact.MailingCity;
a.BillingState = lastContact.MailingState;
a.BillingCountry = lastContact.MailingCountry;
a.BillingPostalCode = lastContact.MailingPostalCode;
accList.add(a);
}
// And you're done!
if(!accList.isEmpty()) {
update accList;
}
}
But I'm getting this error:
TestTrigger: execution of AfterUpdate caused by: System.FinalException: Record is read-only Trigger.TestTrigger: line 31, column 1
Well, like I said I'm new into this apex and SOQL thing and to be honest I don't understand pretty much about it, even so, I need to keep this job by the moment so I would really appreciate all your help.