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
Rana RoyRana Roy 

How to store the names of Contacts into Accounts?

Is there any way to store the List of Contacts on some Field of Account?

Suppose there is a Text Area Long Field on Account where I want to store the names of all the contacts for the account.
 
How to do that?
Best Answer chosen by Rana Roy
EldonEldon
Hi Rana,

Try this trigger on contact
 
trigger Devcomcn3 on Contact (after insert) {
    
    list <id> ids = new list<id>();
    for(contact c : trigger.new){
        ids.add(c.accountid);
    }
    list<account> accounts = [select id,name,conts2__c from account where id in :ids];
    list<account> AccountsToUpdate = new list<account>();
    system.debug('size'+accounts.size());
    for(account a : accounts){
        for(contact c : trigger.new){
            if(c.accountid == a.id){
                system.debug('equal');
                a.conts2__c +=c.lastname;
            }
        }
        AccountsToUpdate.add(a);
    }
    update AccountsToUpdate;
    
}

Let me know if you have any issues.
Mark it as best answer if it works.

Regards