+ Start a Discussion
rv90rv90 

I want to show all the names from a related list object added in account object field.

I have related list under account object for suppose an account record has 5 related recored on a object, can  i get thosed related list in account field?


User-added image
As shown in the above image, this is a related list on Account object, so i need a field on account object and show all the related list names in the field 

for example : 

Account_field1__c =  Contarct name 1 , contract name 2 , contract name 3 ........ contract name 6 .
Account_field1__c =  619754.1STD ,619744STD,619733STD,619730STD,617208.6STD.



is this possible ?
Best Answer chosen by rv90
HARSHIL U PARIKHHARSHIL U PARIKH
Yes it is possible! How?

You need to write a trigger on child object which would fire on After Insert, After Update, After Delete, After UnDelete events.

In below trigger I have Parent Object as Account and Child Object as Contact. I am populating all contacts names one by one on the parent object.

Trigger Code:
 
Trigger PopulatingNames On Contact(After Insert, After Update, After Delete, After UnDelete){
    
    List<Id> parentIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Contact Con : Trigger.New){
            If(Con.AccountId != Null){
                parentIds.add(Con.AccountId);
            }
        }
    }
    If(Trigger.IsDelete){
        For(Contact Con : Trigger.Old){
            If(Con.AccountId != Null){
                parentIds.add(Con.AccountId);
            }
        }
    }
    
    List<Account> parentListToUpdate = New List<Account>();
    
    For(Account act : [Select Id, All_Contacts_Names__c, (Select Id, FirstName, LastName FROM Contacts) FROM Account WHERE Id =:parentIds])
    {
        
        act.All_Contacts_Names__c = '';
        For(Contact EveryCon : act.Contacts)
        {
            act.All_Contacts_Names__c += EveryCon.FirstName + ' ' + EveryCon.LastName + '\n';
        }
        parentListToUpdate.add(act);
    }
    
    
    try{
        If(!parentListToUpdate.IsEmpty()){
            Update parentListToUpdate;
        }
    }
    Catch(Exception e){
        System.debug('Thrown Exception For PopulatingNames Trigger Is::  ' + e.getMessage());
    }
    
    
    
    
    
    
}

If you want to run this trigger first and see the results all you need to do is to create a field named All_Contacts_Names__c on Account object with LongText data type.

Trigger Result:

User-added image

This trigger also would work for all the operation such as if you delete the contact, it would go and delete the appropriate name from parent field. Samething goes for addting, undeleting etc..

Hope it helps and if it solves the query then kindly mark it as Best Answer!
 

All Answers

alibzafaralibzafar
Yes, it can be achieved via Process Builder Flow or Apex Trigger. 
HARSHIL U PARIKHHARSHIL U PARIKH
Yes it is possible! How?

You need to write a trigger on child object which would fire on After Insert, After Update, After Delete, After UnDelete events.

In below trigger I have Parent Object as Account and Child Object as Contact. I am populating all contacts names one by one on the parent object.

Trigger Code:
 
Trigger PopulatingNames On Contact(After Insert, After Update, After Delete, After UnDelete){
    
    List<Id> parentIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Contact Con : Trigger.New){
            If(Con.AccountId != Null){
                parentIds.add(Con.AccountId);
            }
        }
    }
    If(Trigger.IsDelete){
        For(Contact Con : Trigger.Old){
            If(Con.AccountId != Null){
                parentIds.add(Con.AccountId);
            }
        }
    }
    
    List<Account> parentListToUpdate = New List<Account>();
    
    For(Account act : [Select Id, All_Contacts_Names__c, (Select Id, FirstName, LastName FROM Contacts) FROM Account WHERE Id =:parentIds])
    {
        
        act.All_Contacts_Names__c = '';
        For(Contact EveryCon : act.Contacts)
        {
            act.All_Contacts_Names__c += EveryCon.FirstName + ' ' + EveryCon.LastName + '\n';
        }
        parentListToUpdate.add(act);
    }
    
    
    try{
        If(!parentListToUpdate.IsEmpty()){
            Update parentListToUpdate;
        }
    }
    Catch(Exception e){
        System.debug('Thrown Exception For PopulatingNames Trigger Is::  ' + e.getMessage());
    }
    
    
    
    
    
    
}

If you want to run this trigger first and see the results all you need to do is to create a field named All_Contacts_Names__c on Account object with LongText data type.

Trigger Result:

User-added image

This trigger also would work for all the operation such as if you delete the contact, it would go and delete the appropriate name from parent field. Samething goes for addting, undeleting etc..

Hope it helps and if it solves the query then kindly mark it as Best Answer!
 
This was selected as the best answer