You need to sign in to do that
Don't have an account?
Ramana V
concatenate child records field values in self look up
Hi All,
I am trying to update related record field values into parent record in a self lookup. I tried writing a triggrt for this but it is not working. This is my code.
I am trying to update related record field values into parent record in a self lookup. I tried writing a triggrt for this but it is not working. This is my code.
trigger updComments on college__c (after insert, after update, after delete) Set<id> accIdList = new Set<id>(); List<College__c> colllist = new List<College__c>(); for(College__c con : Trigger.new) { accIdList.add(con.College__c); } for(College__c acc : [Select id, name, University__c,Comments__c,Combined_COmments__c, (Select Id, name,University__c,Comments__c,Combined_COmments__c From College__r) From College__c Where Id In : accIdList]) { List<String> lstSrting = new List<String>(); for(College__c con : acc.University__c__r) { lstSrting.add(con.Comments__c); } College__c a = new College__c(); a.id = acc.College__c; a.Combined_COmments__c = String.join(lstSrting, ','); colllist.add(a); } update colllist;
In above code University__c is a self lookup, I am updaing Comments__c child values into Combined_COmments__c parent record.
Can anyone please suggest me where I am doing wrong
Thanks in Advance
First, you have to identify the child records which are inserting, updating, or deleting are belonging to which Parent(ie University__c).
-accIdList.add(con.College__c); should be accIdList.add(con.University__c);
Inside for loop :
for(College__c con : acc.College__r)
{
lstSrting.add(con.Comments__c);
}
trigger updComments on college__c (after insert, after update, after delete){
Set<id> accIdList = new Set<id>();
List<College__c> colllist = new List<College__c>();
for(College__c con : Trigger.new)
{
accIdList.add(con.University__c);
}
colllist = [Select id, name, University__c,Comments__c,Combined_COmments__c,
(Select Id, name,University__c,Comments__c,Combined_COmments__c From College__r Where Comments__c != null)
From College__c Where Id In : accIdList];
Map<Id,List<String>> lstSrting = new Map<Id,List<String>>();
for(College__c clg : colllist)
{
for(College__c con : colllist.College__r)
{
if(con.University__c == clg.Id)
{
lstSrting.put(con.Id,new List<String> { con.Comments__c});
}
}
}
for(College__c cn : colllist){
cn.Combined_COmments__c = String.join(lstSrting.get(cn.Id), ',');
}
update colllist;
}