You need to sign in to do that
Don't have an account?

Trigger to add values from multiple lookup records to parent record
Hi,
I want to collect all the values for a particular field from a multiple lookup records and add it to a field in parent record. Individual values should be comma separated. How should I acieve this through trigger? Thanks in advance!
I want to collect all the values for a particular field from a multiple lookup records and add it to a field in parent record. Individual values should be comma separated. How should I acieve this through trigger? Thanks in advance!
if(con.LastName <> null && con.AccountId <> null)
{
if(Trigger.isInsert || con.LastName <> Trigger.oldMap.get(con.id).LastName)
{
Ids.add(con.AccountId);
}
this part will run when you insert a new record or you update a last name field in Contact object.
if(Trigger.isUpdate && con.AccountId<>Trigger.oldMap.get(con.id).AccountId)
{
Ids.add(con.AccountId);
Ids.add(Trigger.oldMap.get(con.id).AccountId);
}
this part will run when you change Account lookup record on Contact record.
In my org i have checked, it is working fine on Account and contact object.
All Answers
This is for Account object Lookup on Contact Object..
Trigger contactCopy on Contact(after insert, after update, after delete)
{
List<Account> accRec=new List<Account>();
Set<Id> Ids=new Set<Id>();
if(Trigger.isInsert || Trigger.isUpdate)
{
for(Contact con:Trigger.New)
{
if(con.LastName <> null && con.AccountId <> null)
{
if(Trigger.isInsert || con.LastName <> Trigger.oldMap.get(con.id).LastName)
{
Ids.add(con.AccountId);
}
}
if(Trigger.isUpdate && con.AccountId<>Trigger.oldMap.get(con.id).AccountId)
{
Ids.add(con.AccountId);
Ids.add(Trigger.oldMap.get(con.id).AccountId);
}
}
}
if(Trigger.isDelete)
{
for(Contact con:Trigger.old)
{
if(con.LastName <> null)
{
Ids.add(con.AccountId);
}
}
}
if(Ids.size()>0)
{
accRec=[select NKBIT__Contact_Last_Name__c,(select LastName from Contacts) from Account where Id=:Ids];
{
if(accRec.size()>0)
{
for(Account acc : accRec)
{
acc.NKBIT__Contact_Last_Name__c='';
if(acc.Contacts.Size()>0)
{
for(Contact con : acc.Contacts)
{
if(acc.NKBIT__Contact_Last_Name__c == '')
{
acc.NKBIT__Contact_Last_Name__c=con.LastName;
}
else
if(acc.NKBIT__Contact_Last_Name__c <> null)
{
acc.NKBIT__Contact_Last_Name__c +=', '+con.LastName;
}
}
}
}
}
update accRec;
}
}
}
I used your logic for the 'after insert' and 'after update'. It is working for insert but not for update. Not sure why. It is not updating field in parent record.
Can you pls help.
if(con.LastName <> null && con.AccountId <> null)
{
if(Trigger.isInsert || con.LastName <> Trigger.oldMap.get(con.id).LastName)
{
Ids.add(con.AccountId);
}
this part will run when you insert a new record or you update a last name field in Contact object.
if(Trigger.isUpdate && con.AccountId<>Trigger.oldMap.get(con.id).AccountId)
{
Ids.add(con.AccountId);
Ids.add(Trigger.oldMap.get(con.id).AccountId);
}
this part will run when you change Account lookup record on Contact record.
In my org i have checked, it is working fine on Account and contact object.