function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ramana VRamana 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.

 
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

Akshay SAkshay S
Hi Ramana,

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);
}
 
Akshay SAkshay S
Try below code and see whether it works for you or not:

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;
}
Dibbi CamDibbi Cam
I agree with all of the above. Here is the best service for writing essays on any topic https://www.best-essaywriter.com/, especially programming.
William Brown 29William Brown 29
I am passionate about two domains that are programming and history. I am learning Python and Java language simultaneously, and I am studying essay examples about Frederick Douglass because he is one of the significant historical persons that impress me. https://studyhippo.com/essay-examples/frederick-douglass/ is fit for my student necessities of providing info on whatever topic I need, whether it is about slavery and abolitionist heroes of its elimination or something about programming languages.