You need to sign in to do that
Don't have an account?
Shruti Tandon
trigger AccEmail on Contact (after insert,after update) {
String Em='';
if(trigger.isInsert || trigger.isUpdate ){
Set <String> accID = New Set <String> ();
For (Contact con: Trigger.new) {
if (con.AccountId != Null ) {
accID.add (con.AccountId);
}
}
if (accID.size ()> 0) {
List <Account> upAccList = new List <Account> ();
for(Account acc : [Select id, EmailId__c,
(Select Id, Email From Contacts)
From Account Where Id In : accID])
for(Contact con : acc.contacts)
{
if(con.Email != null)
{
Em = Em + ', ' + con.Email;
}
acc.Contact_Names__c = names;
upAccList.add(acc);
}
update upAccList;
}
}
trigger to populate email id of contacts associated with an account on account page (insert,update,delete)
trigger AccEmail on Contact (after insert,after update) {
String Em='';
if(trigger.isInsert || trigger.isUpdate ){
Set <String> accID = New Set <String> ();
For (Contact con: Trigger.new) {
if (con.AccountId != Null ) {
accID.add (con.AccountId);
}
}
if (accID.size ()> 0) {
List <Account> upAccList = new List <Account> ();
for(Account acc : [Select id, EmailId__c,
(Select Id, Email From Contacts)
From Account Where Id In : accID])
for(Contact con : acc.contacts)
{
if(con.Email != null)
{
Em = Em + ', ' + con.Email;
}
acc.Contact_Names__c = names;
upAccList.add(acc);
}
update upAccList;
}
}
I am not able to properly find motive of using "Names" variable here, it would be very helpful if you elaborate things clearly.
But after going through the code, I assume you are trying to populate Contact_Names__c field with email field of Contact.
Please use Em instead of using Names.
trigger PopulateAcc on Contact (before insert,after update) {
String Em='';
if(trigger.isInsert || trigger.isUpdate ){
Set <String> accID = New Set <String> ();
For (Contact con: Trigger.new) {
if (con.AccountId != Null ) {
accID.add (con.AccountId);
}
}
if (accID.size ()> 0) {
List <Account> upAccList = new List <Account> ();
for(Account acc : [Select id, EmailId__c,
(Select Id, Email From Contacts)
From Account Where Id In : accID])
for(Contact con : acc.contacts)
{
if(con.Email != null)
{
Em = Em + ', ' + con.Email;
}
acc.Contact_Names__c = Em;//Names
upAccList.add(acc);
}
update upAccList;
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
I made few updates you missing like Delete, Initializing Em variable per Accounts etc. I have also included the test class with 100% coverage of Insert, Update and Delete.
Test Class
When you say "that is exactly what I am looking ...", I guess you are approving my code.
Please mark that a best answer
I can help you with something else if you can send it over different ticket.
Can you mark the first one as best if it helped. Thanks
Please provide the process how it is functioning?
To this
Also share the code you have so I can match that with mine?