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
Chetan PradhanChetan Pradhan 

Field to display name of all records of an affiliated/related object (separated by commas)

I am new to Salesforce Development. I want to know if there is a way to display name of all records of an affiliated/related object (separated by commas) in one field using Formula field or trigger, etc. I am looking to have something similar to what you see when try to create a new record type (for Contact, say) under Record Types currently available column. Is it possible? My goal is to use that field in a report or list view to display the related record names.
Thanks!
v varaprasadv varaprasad
Hi Chetan,

Please check once following sample code.It will display all child record names in account description field.
 
==================trigger=======================
trigger ContactTrigger_Handler on Contact (after insert,after delete,after update) {
    
    if(trigger.isinsert && trigger.isAfter){
        updaetcontactnamefield.updateaccount(Trigger.new);
    }
    
     if(trigger.isdelete && trigger.isAfter){
        updaetcontactnamefield.updateaccount(Trigger.old);
    }
    
     if(trigger.isupdate && trigger.isAfter && !RecursionCheck.recusionChecks.contains('ContactTrigger_Handler')){   
         RecursionCheck.recusionChecks.add('ContactTrigger_Handler');
        UpdteTime.lastModiieddate(Trigger.new);
    }
}


=====================Class===================

public class updaetcontactnamefield{
    public static void updateaccount(list<contact> contacts){
        set<id> accountIds = new set<id>();
        
        if(!contacts.isEmpty()){
            for(contact c:contacts){
                accountIds.add(c.accountid);
            }
        }
        
        list<account> lstAcc = new list<account>();
        for(Account acc : [Select id, name, description, 
                           (Select Id, name, LastName From Contacts) 
                           From Account Where Id In : accountIds])
        {
            List<String> lstSrting = new List<String>();
            for(Contact con : acc.contacts)
            {
                lstSrting.add(con.lastname);
            }
            acc.description = String.join(lstSrting, ',');
            lstAcc.add(acc);
        }
      update lstAcc;
    }
    
}

Hope this helps you!


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
v varaprasadv varaprasad
Please use below trigger instead of above one : 

 
trigger ContactTrigger_Handler on Contact (after insert,after delete,after update,after undelete) {
    
    if(trigger.isAfter &&(trigger.isinsert || trigger.isupdate || trigger.isUndelete)){
        updaetcontactnamefield.updateaccount(Trigger.new);
    }
    
     if(trigger.isdelete && trigger.isAfter){
        updaetcontactnamefield.updateaccount(Trigger.old);
    }
    
    
}

 
Chetan PradhanChetan Pradhan
Thanks Varaprasad. Looks to make sense to me if it is a direct lookup. 

What changes would be there if I am using a Junction object?. Say B is junction object between contact and account.

Thanks again!
v varaprasadv varaprasad
Hi Chetan,

If you want to display B records in the account or contact No need to change anything.

accountIds.add(c.accountid);
here you need to select which parent object you need to show child records.


Hope this helps you!
If the above information is informative and useful please mark it as the best answer.


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 
Chetan PradhanChetan Pradhan
Thanks Varaprasad, will give it a shot and let you know how it goes. Appreciate your help,