+ Start a Discussion
SteveMouSteveMou 

Apex Trigger on child object to concatenate child record values to a text field on Parent object

All I am trying to write an Apex Trigger that will take a text formula field (Bus_Seg) on a Child Object (Account_Team_Assignment__c) and concatenate the values into a custom long text field (AccountTeamBusSeg__c) on the Parent object (Account). 

The Account_Team_Assignment__c object is tied to the Account object by a lookup field called Account_Name__c.  The field Account_Name__c stores the 18 digit Account ID on each Account Team Assignment record created.

Can anyone help me understand why what I have below is not working properly? I keep getting an error stating "Didn't understand relationship 'Account_Team_Assignment__r".



trigger trgConCatBusSeg on Account_Team_Assignment__c (after insert, after update, after delete, after undelete) {

//If the event is insert or undelete, this list takes New Values or else it takes old values
List<Account_Team_Assignment__c> AccountTeamList = (Trigger.isInsert|| Trigger.isUnDelete) ? Trigger.new : Trigger.old;

//Stores the Account Team assignment object's Account ID that is associated with each child record
List<Account_Name__c> AccountTeamIds = new List<Account_Name__c>();

//Loop through the Account Team Assignment object Records to store the Accountteam Id values
for (Account_Team_Assignment__c Acc_team : AccountTeamList) {
AccountteamIds.add(Acc_team.Account);
}

//Sub-query to get the Account and all its Child Records where Account's Id is equal to the Ids stored in AccountTeamIds
//Account_Team_Assignment__c is the Child Relationship name appended by '__r' as it is a custom object

List<Account> AccountList = [
select
id,
(select Account_Name__c, BUS_SEG__c from Account_Team_Assignment__r)
from
Account
where
id in :AccountTeamIds];

//Loop through the List and store the Child Records as a String of values in Long Text Area Field called AccountTeamBusSeg__c

for (Account Acct : AccountList) {

if(Acct.Account_Team_Assignment__r.size() > 0)
{
Acct.AccountTeamBusSeg__c = string.valueOf(Acct.Account_Team_Assignment__r[0].BUS_SEG__c);

for(integer i=1;i < Acct.Account_Team_Assignment__r.size();i++)
{
Acct.AccountTeamBusSeg__c = Acct.AccountTeamBusSeg__c + '; ' + string.valueOf(Acct.Account_Team_Assignment__r[i].BUS_SEG__c);
}
}
else
Acct.AccountTeamBusSeg__c = null;

}

//update the List
update AccountList;


}
Blake TanonBlake Tanon
It probably needs to be Account_Team_Assignments__r
Blake TanonBlake Tanon
I would rewrite the middle part like this....  in the for loop you were using to gather the bus_seg comments, you were skipping the last record also.

//set of account Id's - lists can contain dupilcates.
set<id> AccountTeamIds = new set<id>();

//Loop through the Account Team Assignment object Records to store the Accountteam Id values
for (Account_Team_Assignment__c Acc_team : AccountTeamList) {
AccountteamIds.add(Acc_team.Account_Name__c);
}

List<Account> AccountList = [select id, (select Account_Name__c, BUS_SEG__c from Account_Team_Assignments__r)
from Account where id in :AccountTeamIds];


for (Account Acct : AccountList) {

if(Acct.Account_Team_Assignments__r.size() > 0){
  Acct.AccountTeamBusSeg__c = '';

  for(integer i=0;i < Acct.Account_Team_Assignments__r.size();i++){
   Acct.AccountTeamBusSeg__c += string.valueOf(Acct.Account_Team_Assignments__r[i].BUS_SEG__c)+ '; ';
  }
  //remove last ;
  Acct.AccountTeamBusSeg__c.replaceLast('; ','');

}else Acct.AccountTeamBusSeg__c = null;

}
SteveMouSteveMou
Thanks Blake. I edited the code to be the below, however now I am getting a "Duplicate Variable AccountTeamIds" error. Any thoughts?


trigger trgConCatBusSeg on Account_Team_Assignment__c (after insert, after update, after delete, after undelete) {

//If the event is insert or undelete, this list takes New Values or else it takes old values
List<Account_Team_Assignment__c> AccountTeamList = (Trigger.isInsert|| Trigger.isUnDelete) ? Trigger.new : Trigger.old;

//Stores the Account Team assignment object's Account ID that is associated with each child record
List<Account_Name__c> AccountTeamIds = new List<Account_Name__c>();


//set of account Id's - lists can contain dupilcates.
set<id> AccountTeamIds = new set<id>();

//Loop through the Account Team Assignment object Records to store the Accountteam Id values
for (Account_Team_Assignment__c Acc_team : AccountTeamList) {
AccountteamIds.add(Acc_team.Account_Name__c);
}

List<Account> AccountList = [select id, (select Account_Name__c, BUS_SEG__c from Account_Team_Assignments__r)
from Account where id in :AccountTeamIds];


for (Account Acct : AccountList) {

if(Acct.Account_Team_Assignments__r.size() > 0){
  Acct.AccountTeamBusSeg__c = '';

  for(integer i=0;i < Acct.Account_Team_Assignments__r.size();i++){
   Acct.AccountTeamBusSeg__c += string.valueOf(Acct.Account_Team_Assignments__r[i].BUS_SEG__c)+ '; ';
  }
  //remove last ;
  Acct.AccountTeamBusSeg__c.replaceLast('; ','');

}else Acct.AccountTeamBusSeg__c = null;

}
Blake TanonBlake Tanon
Change that list to a set, that will prevent dups Blake Tanon
Blake TanonBlake Tanon
Sorry I misread the error, you have a list and set with the same variable name.